Search code examples
javaswingjtableuimanager

How to change row color in Jtable using UIManager


I tried using UIManager to change border color of the focused cell in my JTable, and it works fine. But I don't know how to change the color of rows in my JTable using UIManager. Can anybody tell me what can I write inside UIManager.put(); to change the color of rows.

Here's the code

public class JTableExample 
{    
    JFrame f;  
    JPanel p2;

    JTableExample()
    {    
    f=new JFrame();  
    
    p2=new JPanel(new GridLayout());
    p2.setBackground(Color.white);
    p2.setBounds(0, 40, 946, 561);
    
    String data[][]={ {"0","Raj","67"},    
                          {"1","Pranav","78"},    
                          {"2","Saurabh","70"}};  
  
    String column[]={"ID","NAME","Percentage"};   
      
    JTable jt=new JTable(data,column);    
    UIManager.put("Table.focusCellHighlightBorder",new BorderUIResource(BorderFactory.createLineBorder(Color.red)));
    JScrollPane sp=new JScrollPane(jt);  
    
    p2.add(sp);  
    
    f.setBounds(170, 100, 1250, 600);    
    f.setVisible(true);
    f.add(p2);
    
    }     
    public static void main(String[] args)
    {    
        new JTableExample();    
    }    
}  

Solution

  • Can anybody tell me what can I write inside UIManager.put(); to change the color of rows.

    Generally you would change the properties of the JTable:

    table.setBackground(...);
    table.setSelectionBackground(...);
    

    If you really want to try to change the defaults for all JTables in your application then check out UIManager Defaults. It provides a list of properties that can be changed for each Swing component.