Java – swing – JTable – sets the color for the selected row, but not for the cell
I try to make my table select the entire row when you click a cell (this can be done by turning off column selection), but I don't want the extra thick border around the specific cell you click to highlight I hope it's easy, but obviously it involves a renderer, so I've done a lot of research. The closest I can get is:
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 color I want Setting the color in isselected() does nothing
The problem with this code is:
>It applies only to the column specified by vcolindex at the bottom Obviously, I want to apply it to all columns, so clicking a cell highlights the entire row I can create a for loop to change it to each cell, but I think it's a better way to change the cellrenderer of all columns immediately. > Setforegroundcolor() is used to change the text, but setbackgroundcolor() does nothing with the cell background I hope it actually changes the background color implied by the attribute
>#2 solution: use this setOpaque(true); Before assigning backgroundColor
>When the renderer works, it applies only to a single cell How do I make it color all cells in a row?
>#3 solution: I see! If row selection (table. Setrowselectionallowed (true)) is enabled, the color change is placed in the if (isselected) statement instead of using hasfocus(), which affects only a single cell Then the whole row is considered selected and it will color all cells!
3 is the most important, but I would appreciate it if someone knew #1 or could explain to me why it is designed so that you can only apply the renderer to one column at a time
Solution
The tutorial article concepts: editors and renderers explains that "a single cell renderer is usually used to draw all cells containing the same type of data", as returned by the getcolumnclass () method of the model
Alternatively, override the preparereader() called for all cells to selectively change the appearance of rows Example compares the two methods. The article table row rendering extends the versatility of this method