简体   繁体   中英

JTabbedPane.setBackgroundAt(index, Color) after setVisible(true) has no effect

I'm trying to create a JTabbedPane with markers for the tabs that need the user's attention. It is going to be used for a settings dialog.
This means the background color should be changeable dynamically and independent of tab selection.

The validation for the elements is working (eg red border around invalid textfields), but I have trouble to mark the containing tab by setting a background color. The code to find the containing tab and propagate the invalid state is ready. The only thing is: setBackgroundAt(index, Color); has no effect at all.
I have verified that the statement is reached and the index is correct. But nothing happens. Neither repaint(); nor SwingUtilities.updateComponentTreeUI(this); did help.

I have found several examples of people giving different colors to different tabs. This is the one I started with: Colorize a tab in a JTabbedPane using java swing

But all of the examples I found set the color statically (before showing the JFrame) and what is even worse, the background color disappears for the selected tab.

As my application also allows to select the look & feel, the solution should be L&F independent.

BTW: A red border around the tab instead of a background will do too.

While "the solution should be L&F independent," the tabbed pane UI delegate exerts considerable control over the tab's appearance. A given delegate is free to ignore your color setting in favor of its own. As the goal is "markers for the tabs that need the user's attention," also consider a custom component or icon, seen in the tutorial . In particular, TabComponentsDemo illustrates adding an interactive component to the tab, and this example illustrates an animated icon.

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