문제

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

도움이 되었습니까?

해결책

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)

다른 팁

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());

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()); 
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top