简体   繁体   中英

Setting background of tabs in JTabbedPane

I am trying to set a background color to the tabs of a JTabbedPane:

This works:

 tabbedPane.setBackgroundAt(idx, Color.GREEN);

However, it only works for the deselected tabs. Is there a way to do this for the active tab, too? I do not want to provide a totally new UI for this.

I think it's clear you've found a bug. I don't see anything in the Java Bug Database that addresses this, but I see some other bugs related to JTabbedPane background colors that are fixed for Java 9 (like http://bugs.java.com/bugdatabase/view_bug.do?bug_id=8078269 ).

This isn't pretty, as modifying the UI defaults means it will affect all subsequently created JTabbedPanes, but it does work:

Runnable tabColorUpdater = new Runnable() {
    @Override
    public void run() {
        UIManager.put("TabbedPane.selected",
            tabbedPane.getBackgroundAt(tabbedPane.getSelectedIndex()));
        tabbedPane.updateUI();
    }
};

tabColorUpdater.run();
tabbedPane.addChangeListener(
    e -> EventQueue.invokeLater(tabColorUpdater));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM