Skip to Main Content

Java SE (Java Platform, Standard Edition)

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Unable to set background colour JTableHeader in Java 1.6_18

843807Feb 2 2010 — edited Feb 2 2010
We're moving our application from Java 1.4.2 to 1.6_18, so far without major problems but there's some small issues with the GUI. Originally we'd change the background colour of the JTableHeader, but in 1.6_18 that doesn't seem to work. Instead of colouring the complete header it just seems to draw an outline. It kind of gives the impression that there's something on top of the table header which hides the coloured background.

The code below shows what I'm trying to do, and gives different results when compiled in Java 1.4.2 and Java 1.6_18. It makes no difference whether I'm using the UIManager or setting the background directly on the JTableHeader.

The application still works fine of course, but it bugs me that I can't work out what the problem is, so any suggestions would be greatly appreciated! I've already had a look at bug [6521138|http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=6521138], but it makes no difference whether a new JTableHeader is set, so I think it's different. Also, the workaround for bug [4776303|http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=4776303] has no effect so I think that doesn't apply either.

    public static void main(String[] args) {
        try {
            UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
            // Doesn't give the expected result in Java 1.6_18, the background remains white-ish with only a coloured border.
            // UIManager.put("TableHeader.background", Color.GREEN);
            // UIManager.put("TableHeader.foreground", Color.BLUE);

        } catch (Exception exception) {
            exception.printStackTrace();
        }

        final JFrame frame = new JFrame();

        frame.getContentPane().add(createPanel());

        frame.getContentPane().setLayout(new FlowLayout());
        frame.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
        frame.pack();
        frame.setVisible(true);
    }

    private static JPanel createPanel() {
        final JPanel panel = new JPanel();

        final DefaultTableModel model = new DefaultTableModel(3, 2);
        final JTable table = new JTable(model);
        // Tableheaders.
        final TableColumnModel columnModel = table.getColumnModel();
        columnModel.getColumn(0).setHeaderValue("First");
        columnModel.getColumn(1).setHeaderValue("Second");

        // Doesn't give the expected result in Java 1.6_18, the background remains white-ish with only a coloured border.
        table.getTableHeader().setBackground(Color.GREEN);
        table.getTableHeader().setForeground(Color.BLUE);

        final JScrollPane scrollPane = new JScrollPane(table);
        panel.add(scrollPane);
        scrollPane.setSize(400, 100);

        panel.setSize(500, 500);
        panel.setPreferredSize(panel.getSize());

        return panel;
    }
 
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Mar 2 2010
Added on Feb 2 2010
10 comments
905 views