1

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 (e.g. 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.

Community
  • 1
  • 1
Markus N.
  • 491
  • 4
  • 7
  • 2
    Are you sure the tab background is not entirely covered by the component it contains? For better help sooner, post an SSCCE. – JB Nizet Nov 12 '13 at 21:00
  • 2
    `SwingUtilities.updateComponentTreeUI(this);` you should never need to invoke this method yourself, this is done by the system on a LAF change. – camickr Nov 12 '13 at 21:01
  • 2
    The tab color is only painted if the L&F respects it; most do not. – Russell Zahniser Nov 12 '13 at 21:01
  • @JB Nizet Yes, I am sure: the static colors that are set before setVisible(true) according to the example I referred to are working. – Markus N. Nov 12 '13 at 21:14
  • @camickr the SwingUtilities.updateComponentTreeUI(this); was an act of desparation :-) but when changing the L&F without resstart, I have experienced problems without this statement. – Markus N. Nov 12 '13 at 21:18
  • @Russell Zahniser well, that's bad news. But I have just verified this by testing my static color example with different L&F's – Markus N. Nov 12 '13 at 21:20
  • The [example cited](http://stackoverflow.com/a/11334283/230513) works on Mac & Linux. What L&F? – trashgod Nov 12 '13 at 21:23
  • @trashgod With Metal the static colors work, with JGoodies Plastic they don't. Both tried on Linux – Markus N. Nov 12 '13 at 21:40

1 Answers1

2

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.

Community
  • 1
  • 1
trashgod
  • 203,806
  • 29
  • 246
  • 1,045
  • Yes, I'm going for an icon, probably something similar to an "attention" traffic sign (trinagle shaped with exclamation mark) – Markus N. Nov 12 '13 at 22:58
  • @MarkusN.: Sounds reasonable; you may be able to leverage an existing `UIManager` icon, for [example](http://stackoverflow.com/a/12228640/230513), although sizes vary. – trashgod Nov 13 '13 at 03:40