0

I want a button when clicked, calls upon a method that changes a jTable object's cell colors. Something like:

jTableName.setCellBackground(Color, row, column)

Bringing me to a method as close or similar to this would really help me right now.

eldo
  • 1,327
  • 2
  • 16
  • 27
AminoAcid
  • 188
  • 9

1 Answers1

0

This might help you

You can use DefaultTableCellRenderer to color alternate row from JTable.

table.setDefaultRenderer(Object.class, new TableCellRenderer(){ private DefaultTableCellRenderer DEFAULT_RENDERER = new >DefaultTableCellRenderer();

       @Override
       public Component getTableCellRendererComponent(JTable table, Object >value, boolean isSelected, boolean hasFocus, int row, int column) {
           Component c = >DEFAULT_RENDERER.getTableCellRendererComponent(table, value, isSelected, >hasFocus, row, column);
           if(isSelected){
               c.setBackground(Color.YELLOW);
           }else{
           if (row%2 == 0){
               c.setBackground(Color.WHITE);

           }
           else {
               c.setBackground(Color.LIGHT_GRAY);
           }     }

  //Add below code here
           return c;
       }

   });

If you want to color your row using the value of a particular row then you can >use something like this. Add these line to above

if(table.getColumnModel().getColumn(column).getIdentifier().equals("Status")){//HereStatusis column name if(value.toString().equals("OK")){//HereOKis the value of row

   c.setBackground(Color.GREEN);

}

This is from here

Community
  • 1
  • 1
Sorx
  • 23
  • 1
  • 8