Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change the background color of a row in a JTable

Tags:

I have a JTable with 3 columns. I've set the TableCellRenderer for all the 3 columns like this (maybe not very effective?).

 for (int i = 0; i < 3; i++) {      myJTable.getColumnModel().getColumn(i).setCellRenderer(renderer);  } 

The getTableCellRendererComponent() returns a Component with a random background color for each row.
How could I change the background to an other random color while the program is running?

like image 856
user Avatar asked Oct 06 '10 18:10

user


People also ask

How do I change the appearance of data in a JTable cell?

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.

How do you make a JTable cell editable?

jTableAssignments = new javax. swing. JTable() { public boolean isCellEditable(int rowIndex, int colIndex) { return editable; }};

How can I tell if a row is selected in JTable?

So you can call table. getSelectionModel(). isSelectionEmpty() to find out if any row is selected.


2 Answers

Resumee of Richard Fearn's answer , to make each second line gray:

jTable.setDefaultRenderer(Object.class, new DefaultTableCellRenderer() {     @Override     public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column)     {         final Component c = super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);         c.setBackground(row % 2 == 0 ? Color.LIGHT_GRAY : Color.WHITE);         return c;     } }); 
like image 103
Olivier Faucheux Avatar answered Sep 23 '22 13:09

Olivier Faucheux


One way would be store the current colour for each row within the model. Here's a simple model that is fixed at 3 columns and 3 rows:

static class MyTableModel extends DefaultTableModel {      List<Color> rowColours = Arrays.asList(         Color.RED,         Color.GREEN,         Color.CYAN     );      public void setRowColour(int row, Color c) {         rowColours.set(row, c);         fireTableRowsUpdated(row, row);     }      public Color getRowColour(int row) {         return rowColours.get(row);     }      @Override     public int getRowCount() {         return 3;     }      @Override     public int getColumnCount() {         return 3;     }      @Override     public Object getValueAt(int row, int column) {         return String.format("%d %d", row, column);     } } 

Note that setRowColour calls fireTableRowsUpdated; this will cause just that row of the table to be updated.

The renderer can get the model from the table:

static class MyTableCellRenderer extends DefaultTableCellRenderer {      @Override     public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {         MyTableModel model = (MyTableModel) table.getModel();         Component c = super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);         c.setBackground(model.getRowColour(row));         return c;     } } 

Changing a row's colour would be as simple as:

model.setRowColour(1, Color.YELLOW); 
like image 37
Richard Fearn Avatar answered Sep 22 '22 13:09

Richard Fearn