I am trying to make my table select an entire row when you click on a cell (which can be done by turning off column select), but, I don't want the extra thick border around the specific cell you clicked to be highlighted. I was hoping this would be easy but apparently it involves renderers so I did a lot of research and the closest I can get is this:
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 an 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 only works on one column specified by vColIndex at the bottom. Obviously I want this applied to all columns so clicking on a cell in one highlights the entire row. I could make a for loop to change it to every cell but I figure there's a better way of doing this that changes the cellRenderer for ALL columns at once.
setForegroundColor()
works to change text but setBackgroundColor()
does nothing to the cells background. I would like it to actually change the background color like the property implies.
this.setOpaque(true);
before assigning backgroundcolor.When the renderer does work, it only works on a single Cell. How can I get it to color all the Cells in the row?
hasFocus()
, which only affects the single cell, if you enable row selection (table.setRowSelectionAllowed(true)
) then you put the color changing in the if(isSelected)
statement. Then the whole row is considered selected and it colors all the cells in!3 was the big one but if anyone knows #1 or can explain to me why it was designed such that you can only apply the renderer to one column at a time it would be much appreciated.
In java, there is facility to change selection color of particular rows in jtable according to requirement. MyTblView .setSelectionBackground (Color. BLACK ); MyTblView .setSelectionForeground (Color. YELLOW );
We can change the background and foreground color for each column of a JTable by customizing the DefaultTableCellRenderer class and it has only one method getTableCellRendererComponent () to implement it.
Java Swing | JTable. The JTable class is a part of Java Swing Package and is generally used to display or edit two-dimensional data that is having both rows and columns. It is similar to a spreadsheet.
/** * GrayWhiteTable renders its rows with a sequential color combination of white * and gray. Rows with even indicies are rendered white, odd indicies light grey. * Note: Do not use GrayWhiteTable for tables with custom renderers such as * check boxes. Use JTable instead and modify DefaultTableCellRenderer.
too direct just do add line
tablename.setSelectionBackground(Color.red);
in my case
jtbillItems.setSelectionBackground(Color.red);
The tutorial article Concepts: Editors and Renderers explains that "a single cell renderer is generally used to draw all of the cells that contain the same type of data," as returned by the model's getColumnClass()
method.
Alternatively, override prepareRenderer()
, which is invoked for all cells, to selectively alter a row's appearance. This example compares the two approaches, and the article Table Row Rendering expands on the versatility of the approach.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With