how to select the entire row of a Jtable using Defaultcellrenderer which changes the color of the line

Asked

Viewed 1,071 times

1

Hello, I’m working with Jtables and on such a Jtable I needed to leave some lines in differentiated colors, so I implemented the method

class cellRenderModel extends DefaultTableCellRenderer {

/**
 *
 */
private static final long serialVersionUID = 1L;

ModelCartao mCard;

public cellRenderModel(ModelCartao mc) {
    this.mCard = mc;
}

@Override
public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
    JLabel label = (JLabel) super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
    Color c = Color.WHITE;
    if (mCard.isCor(row)) {//Verifica se é para pintar ou não (funcionando perfeitamente!)
        c = Color.GREEN;
        label.setBackground(c);
    } else {
        c = Color.WHITE;
        label.setBackground(c);
    }
    return label;
}
}

And left as table defaultRender

minhaTabela.setDefaultRenderer(Object.class,new cellRenderModel(mCard));

The problem:

before the implementation of this method, as selected by Jtable, he selected the entire line (leave the selected line in blue) as is the Jtable standard. However, after formatted as described, it lost this feature, I would like it to come back as before, that it selects the entire line as it clicks or presses up or down.

And another problem: when I click on the table, it shows a "focus" on the cell, like a blue slightly more emblematic than the common selection, around such a cell, but when the table loses focus, this feature also disappears, only coming back when it regains focus again, I’d like to make that appear all the time. and if possible, change also.

NOTE: I tried to add this mouseListener but it didn’t work:

minhaTabela.addMouseListener(new MouseListener() {
        @Override
        public void mouseReleased(MouseEvent e) {
            int row = minhaTabela.getSelectedRow();
            int col = minhaTabela.getSelectedColumn();
            tbFin.setRowSelectionInterval(row,row);
            tbFin.setColumnSelectionInterval(col,col);
        }

        @Override
        public void mouseClicked(MouseEvent e) {
        }

        @Override
        public void mousePressed(MouseEvent e) {
        }

        @Override
        public void mouseEntered(MouseEvent e) {
        }

        @Override
        public void mouseExited(MouseEvent e) {
        }
    });

here are my Jtable’s "settings":

tb.getTableHeader().setReorderingAllowed(false);
    tb.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
    tb.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
    tb.getTableHeader().setResizingAllowed(false);
    tb.setColumnSelectionAllowed(true);
    tb.setRowSelectionAllowed(true);
    tb.setDefaultRenderer(Object.class, new celRenderModel(mCard));
    tb.setCellSelectionEnabled(false);
    tb.setRowSelectionAllowed(true);

From Now, thank you!

  • This doesn’t feel right: JLabel label = (JLabel) super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column); What is the need for this?

  • this serves to treat the specific cell as a Jlabel, facilitating the way to leave the background different color

  • To select an entire, single line, one suaTable.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);

  • was with MULTIPLE_INTERVAL_SELECTION, switched to your tip, made no difference :/

  • I put my Jtable’s "settings" in doubt, hope it helps to know what I’m doing wrong

  • I recommend you change the first line of your Nderer, leave it super isolated. Do the test to see if it works again.

  • I don’t know if this is what you suggested, but following "more or less" your logic of reasoning, I managed to solve, I will post the code as an answer, thanks even Diego!

Show 2 more comments

3 answers

2


way I found to solve my problem:

class celRenderModel extends DefaultTableCellRenderer {

/**
 *
 */
private static final long serialVersionUID = 1L;

ModelCartao mCard;

public celRenderModel(ModelCartao mc) {
    this.mCard = mc;
}

@Override
public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {

    Color c = Color.WHITE;
    if (mCard.isCor(row)) {//Verifica se é para pintar ou não (funcionando perfeitamente!)
        c = Color.GREEN;
        setBackground(c);
    } else {
        c = Color.WHITE;
        setBackground(c);
    }
    return super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
}
}

I hope this helps anyone with that same doubt!!!

  • In fact, you can remove the super in the first line of the method, because you are already triggering it in Return, it will continue to work. the same way.

  • 1

    It worked too, I’ll edit the answer, thanks for the tip!!

0

It’s simple. Delete these lines below. One of them must have been created automatically by the IDE in the table construction:

jTable.getColumnModel(). getSelectionModel(). setSelectionMode(javax.swing.Listselectionmodel.MULTIPLE_INTERVAL_SELECTION);

OR jTable.getColumnModel(). getSelectionModel(). setSelectionMode(javax.swing.Listselectionmodel.SINGLE_SELECTION);

OR

jTable.getColumnModel(). getSelectionModel(). setSelectionMode(javax.swing.Listselectionmodel.SINGLE_INTERVAL_SELECTION);

In doing so, the entire line is selected.

0

  • Thanks, but I was already with setRowSelectionAllowed(true), as I said, before I add the Defaultrenderer worked the selection normally, but after adding, complicated a little

  • And as for the links, I appreciate it, but I’ve read these two links about 30x to do this project, and it’s all been going well, so far :/

Browser other questions tagged

You are not signed in. Login or sign up in order to post.