how to select the entire row of a JTable using DefaultCellRenderer that changes the color of the line

1
Hello, I'm working with JTables, and in that JTable I needed to leave some lines in different 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's defaultRender

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

The problem:

Before implementing this method, as you selected JTable, it selected the entire line (leave the selected line in blue), as is the JTable pattern. However, after formatting as described, he lost that feature, would like it to go back as before, let him select the entire line as he 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 a little more emblematic than the common selection, around such a cell, but when the table loses focus, this characteristic also disappears, coming back only when it gains focus again, I would like to make it appear always. and if possible, change as well.

NOTE: I already tried to add this mouseListener but it did not 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 the "settings" of my JTable:

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);

Now, thank you!

    
asked by anonymous 23.05.2016 / 22:03

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 this same question!

    
24.05.2016 / 18:32
0

Have you tried setRowSelectionAllowed(true) on your JTable instance?

I suggest you take a look at the Java documentation link

And also read this tutorial sbore the Table Components in Java link

I hope this has helped you:)

    
24.05.2016 / 15:24
0

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

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);

By doing this, the entire line is selected.

    
26.07.2016 / 05:40