Java - Swing - JTable - setting the color for the selected row, but not the cell

I try to make my table a selected whole row when you click on a cell (which can be done by disabling the column selection), but I do not want the extra thick border around the specific cell you clicked to be highlighted. I was hoping this would be easy, but apparently this is due to the renderers, so I did a lot of research, and the closest I can do it:

JTable contactTable = new JTable(tableModel); contactTable.setCellSelectionEnabled(true); contactTable.setColumnSelectionAllowed(false); contactTable.setRowSelectionAllowed(false); contactTable.setSelectionMode(ListSelectionModel.SINGLE_SELECTION); // This renderer extends a component. It is used each time a // cell must be displayed. class MyTableCellRenderer extends JLabel implements TableCellRenderer { // This method is called each time a cell in a column // using this renderer needs to be rendered. public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int rowIndex, int vColIndex) { // 'value' is value contained in the cell located at // (rowIndex, vColIndex) if (isSelected) { // cell (and perhaps other cells) are selected } if (hasFocus) { // this cell is the anchor and the table has the focus this.setBackground(Color.blue); this.setForeground(Color.green); } else { this.setForeground(Color.black); } // Configure the component with the specified value setText(value.toString()); // Set tool tip if desired // setToolTipText((String)value); // Since the renderer is a component, return itself return this; } // The following methods override the defaults for performance reasons public void validate() {} public void revalidate() {} protected void firePropertyChange(String propertyName, Object oldValue, Object newValue) {} public void firePropertyChange(String propertyName, boolean oldValue, boolean newValue) {} } int vColIndex = 0; TableColumn col = contactTable.getColumnModel().getColumn(vColIndex); col.setCellRenderer(new MyTableCellRenderer()); 

I copied the Renderer from the example and only changed the hasFocus () function to use the colors I wanted. Setting colors in isSelected() did nothing.

The problem with this code is:

  • It works in only one column specified by vColIndex at the bottom. Obviously, I want this to apply to all columns, so clicking on a cell in one selects the entire row. I could do a for loop to change it for each cell, but I believe that this is the best way to do this, which immediately changes the cellRenderer columns for EVERYTHING.

  • setForegroundColor() works to change the text, but setBackgroundColor() does nothing for the background of the cells. I would like it to really change the background color, as the property implies.

    • Solution for # 2: use this.setOpaque(true); before assigning backgroundcolor.
  • When rendering works, it only works with one cell. How can I make it color all cells in a row?

    • Solution for No. 3: I figured it out! Instead of using hasFocus() , which affects only one cell, if you enable row selection ( table.setRowSelectionAllowed(true) ), you put the color in the if(isSelected) . Then the whole row is considered selected and it paints all the cells in!

3 was great, but if someone knows No. 1 or can explain to me why it was designed so that you can only apply the render to one column at a time, that would be very appreciated.

+6
source share
3 answers

The Tutorial article Concepts: Editors and Renders explains that "rendering of individual cells is usually used to draw all cells containing the same data type" returned by the getColumnClass() model method.

Alternatively, override prepareRenderer() , which is called for all cells, selectively change the appearance of the row. This example compares the two approaches, and Table Row Rendering extends the versatility of the approach.

+5
source

too straightforward just add a line

 tablename.setSelectionBackground(Color.red); 

in my case

 jtbillItems.setSelectionBackground(Color.red); 
+12
source

For the second question, you can try the setSelectionBackground (Color) and setSelectionForeGround (Color) methods. I am not sure how you can solve the first one. And the last sentence, you can use some kind of JBuilder swing designer plug-in. That would help a lot.

0
source

Source: https://habr.com/ru/post/912426/


All Articles