Different color in header of JTable

1.9k Views Asked by At

I have encountered a problem where if I use the main method in the same java file that contains the code for the JFrame which contains a JTable, and where I set the header color to yellow, when I run it , the header will be yellow. However, if I instantiate the JFRame by calling it from other classes, the header will not be yellow but will be the default color instead. Is there any way to ensure that the color stays the same even though I instantiate the JFrame and hence JTable from other classes? Thanks alot! I will be glad to clarify things if I have not explained clearly.

Hi I am sorry for not providing code beforehand. Here it is :

EquityFrame eq= new EquityFrame(file,"Apr2012");
this.dispose();// this code is in another class of a JFrame which will call the constructor of EquityFrame class

Code of EquityFrame class

    public EquityFrame(File file, String nameTab){
    createAndShowGUI( file,  nameTab);
}

    private  void createAndShowGUI(File file, String nameTab){
    //create frame

    JTabbedPane  tabPane= new JTabbedPane();
    //pre-processing
    init(file,nameTab);
    //adding tabs
    tabPane.addTab("Proposal", makeAdminPanel());
    JFrame.setDefaultLookAndFeelDecorated(true);
    JFrame jf= new JFrame("CGH Equity Program");
    jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    jf.setLayout(new BorderLayout());
    jf.add(tabPane,BorderLayout.CENTER);
    int w = Toolkit.getDefaultToolkit().getScreenSize().width;
    int h = Toolkit.getDefaultToolkit().getScreenSize().height;
    jf.setSize(w, h);
    jf.setVisible(true);
}
private JPanel makeAdminPanel(){
    JPanel jp=new JPanel();
    String[] column = {"Job Grade", "Job Title", "Min", "Midpoint", "Max", 
        "Lowest", "P10", "P25", "Median", "P65", "P75", "P90", 
        "Highest", "Average"};
    String[][] data= getArrayOfValuesForEachJobGrade();
    jp.setLayout(new BorderLayout());
    JTable jt= new JTable(data,column);
    JTableHeader th=jt.getTableHeader();
    th.setBackground(java.awt.Color.pink);
    th.setEnabled(false);
    jt.setTableHeader(th);
    jt.setEnabled(false);
    jp.add(jt,BorderLayout.CENTER);
    JScrollPane scrollPane = new JScrollPane(jt, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
    scrollPane.setEnabled(false);
    jp.add(scrollPane, BorderLayout.CENTER);
    return jp;
}

I will first run the first java file which will create an instance of EquityFrame that will display a tabbedPane with a JTable in it. However, the GUI I get from running the EquityFrame with its own main method is different from the GUI I get from creating an instance of it in another code. If i run it using its own main method, there will be a color change in the header of the table. However, the color remains the default if I run it from the other java class.

If I run it using its main method: http://tinypic.com/r/2r5yjdj/6

If I run it using other class to call its constructor to generate the JFrame: http://tinypic.com/r/3523yax/6

Thanks once again for any help rendered! Hope that this sheds more light on my problem.

2

There are 2 best solutions below

1
On BEST ANSWER

Be certain to change the UI property before constructing anything that relies on the new value, preferablely before starting the event dispatch thread.

UIManager.put("TableHeader.background", Color.yellow);
EventQueue.invokeLater(new Runnable() {

    @Override
    public void run() {
        new Application();
    }
});

Alternatively, you may be able to adapt the approach shown here in the method applyUI(); it can be invoked in the constructor, as shown, or when the system calls updateUI().

Addendum: Your first image shows a Look & Feel that supports the TableHeader.background UI property. The second image appears to be the Nimbus L&F, which does not support the property.

3
On

Is the code which set the color in the definition of your main method? If so, put it in the constructors for the JFrame or JTable instead.