1

I have founds loads of examples that change the background color of JTabbedPane using either setBackgroundAt() and UIManager.put("JTabbedPane...")

However, I want to create an onclick event on a checkbox that changes the background color to green when you select it, and back to default when you unselect it.

I haven't been able to make that work using the above methods.

Any ideas?

PS: I can change the foreground color by using setForgroundAt() but not the background for some reason

Roman C
  • 49,761
  • 33
  • 66
  • 176
Shervin Asgari
  • 23,901
  • 30
  • 103
  • 143
  • 2
    works, sure UIManager has more Keys for JTabeedPane, not good JComponent for CustomWhatever, because most of methods are protected from outside(implemented methods too), for better help sooner post an [SSCCE](http://sscce.org/), – mKorbel Oct 26 '12 at 11:37
  • [my view](http://stackoverflow.com/a/11334091/714968) – mKorbel Oct 26 '12 at 11:44
  • I wonder why foreground color can be changed, but not background without implementing my own tabbedpane version – Shervin Asgari Oct 26 '12 at 11:50
  • 2
    don't know whats happened, post an SSCCE – mKorbel Oct 26 '12 at 12:00
  • 1
    Generally, when you can set the foreground, but not the background, you need to check the `opaque` property. If it's false, then the background is not being painted and your are seeing the parent's background. – Devon_C_Miller Oct 26 '12 at 16:13

3 Answers3

4

LAFs are free to ignore custom settings of some (visual only? don't know) JComponent properties, as documented f.i.:

It is up to the look and feel to honor this property, some may choose to ignore it.

So the outcome is highly LAF-dependent (Worksforme in Metal and Motif, not in Nimbus/Win) No easy and safe way around (except tweaking the ui delegate, which isn't a real option)

kleopatra
  • 51,061
  • 28
  • 99
  • 211
  • 1
    Same for all [`JComponent`](http://docs.oracle.com/javase/7/docs/api/javax/swing/JComponent.html#setBackground%28java.awt.Color%29). – trashgod Oct 26 '12 at 19:35
  • @trashgod thanks for the heads up, was overly focused on this particular context ;-) – kleopatra Oct 27 '12 at 10:44
  • So basically give it up I guess. I guess I can only change the foreground color and live with it without changing the look and feel – Shervin Asgari Oct 30 '12 at 09:12
2

Override the paintComponent and change the color there.

@Override
public void paintComponent(Graphics g) {
    g.setColor(new Color(color));
    g.fillRect(0, 0, getWidth(), getHeight());
Roman C
  • 49,761
  • 33
  • 66
  • 176
  • I don't think that works for changing the background color in runtime the way I want. This only changes it once. I need to change it twice using actionlistener – Shervin Asgari Oct 26 '12 at 11:40
  • 1
    @Shervin simply wrap those method calls in an if-statement where the condition checks the value of your checkbox. In your action listener, simply call `repaint()` on the JTabbedPane – Guillaume Polet Oct 26 '12 at 14:17
  • @Shervin Definitely that you change the color somewhere in the action and as @GuillaumePolet said call `repaint` to changes take effect. – Roman C Oct 26 '12 at 17:07
0

Try the following after setting the background/foreground colours of each of your tab panels. This should make the tabs at the top the same colour as the panels in the JTabbedPane (myTabs). This works for me with Nimbus.

for (int c = 0; c < myTabs.getComponentCount(); ++c)
{
  myTabs.setBackgroundAt(c, myTabs.getComponentAt(c).getBackground());
  myTabs.setForegroundAt(c, myTabs.getComponentAt(c).getForeground());
}

myTabs.setOpaque(true);
myTabs.setUI(new BasicTabbedPaneUI()); 
ttpa
  • 1