Note that there are some explanatory texts on larger screens.

plurals
  1. POrefreshing background color for a row in jtable
    primarykey
    data
    text
    <p>i am trying to set the color of row of a Swing Jtable. i use this class to extend Jtable as suggest on the net.</p> <pre><code> public class ColorTable extends JTable { private static final long serialVersionUID = 1L; private Map rowColor = new HashMap(); private Map columnColor = new HashMap(); private Color cellColor; private Color defaultColor; public ColorTable( TableModel model ) { super( model ); } public void setRowColor( int row, Color c) { rowColor.put( new Integer( row ), c ); } public void setColumnColor( int column, Color c ) { columnColor.put( new Integer( column ), c ); } public void setCellColor( Color c ) { cellColor = c; } public Class getColumnClass(int column) { return getValueAt(0, column).getClass(); } public Component prepareRenderer( TableCellRenderer renderer, int row, int column ) { Component c = super.prepareRenderer( renderer, row, column ); if ( defaultColor == null ) defaultColor = c.getBackground(); // Color order is as follows: // rowSelection, checkBox toggle for row color, column color, cell color if ( ! isRowSelected( row ) ) { Color color = (Color) rowColor.get( new Integer( row ) ); if ( color == null || Boolean.FALSE.equals( getModel().getValueAt( row, 0 ) ) ) color = (Color) columnColor.get( new Integer( column ) ); if ( color == null ) { // cell color only if cell has special value, for example purposes, // if the cell value begins with a 2 Object value = getValueAt( row, column ); if ( value != null &amp;&amp; value.toString().startsWith( "2" ) ) color = cellColor; } if ( color != null ) c.setBackground( color ); else c.setBackground( defaultColor ); } return c; } public void resetColor(Color color) { for(int i=0;i&lt;this.getRowCount();i++) this.setRowColor(i, color); } } </code></pre> <p>I simply added the method resetColor(Color color) in order to initialize all the row with the same color.</p> <p>It seams to work at first use but when i want to change the colors i get problems. for instance if I execute the sollowing code within a button action listener i color the table properly just at the first iteration and after never changes the background.</p> <pre><code>deployTable.resetColor(Color.green); // set red background to the for (Integer x : indexes) { System.out.println("index "+x+" red!"); deployTable.setRowColor(x, Color.red); } deployTable.revalidate(); </code></pre> <p>does anyone has any idea about what could it be??</p> <p>Thanks, Ste</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload