6

I know you can modify the LaF properties, but how do you accomplish this without doing such? I only ask because setBackground doesn't seem to do it.

Note that I'm looking to change the following properties:

  1. TabbedPane.background (or TabbedPane.contentAreaColor?)
  2. TabbedPane.tabAreaBackground
mre
  • 43,520
  • 33
  • 120
  • 170

2 Answers2

17

Using TabComponentsDemo as an example, setBackgroundAt() seems to work:

private void initTabComponent(int i) {
    pane.setTabComponentAt(i, new ButtonTabComponent(pane));
    pane.setBackgroundAt(i, Color.getHSBColor((float)i/tabNumber, 1, 1));
}

Addendum: As @camickr helpfully observed, the target component must be opaque.

TabColors

import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTabbedPane;

/** @see http://stackoverflow.com/questions/8752037 */
public class TabColors extends JPanel {

    private static final int MAX = 5;
    private final JTabbedPane pane = new JTabbedPane();

    public TabColors() {
        for (int i = 0; i < MAX; i++) {
            Color color = Color.getHSBColor((float) i / MAX, 1, 1);
            pane.add("Tab " + String.valueOf(i), new TabContent(i, color));
            pane.setBackgroundAt(i, color);
        }
        this.add(pane);
    }

    private static class TabContent extends JPanel {

        private TabContent(int i, Color color) {
            setOpaque(true);
            setBackground(color);
            add(new JLabel("Tab content " + String.valueOf(i)));
        }

        @Override
        public Dimension getPreferredSize() {
            return new Dimension(320, 240);
        }
    }

    private void display() {
        JFrame f = new JFrame("TabColors");
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.add(this);
        f.pack();
        f.setLocationRelativeTo(null);
        f.setVisible(true);
    }

    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {

            @Override
            public void run() {
                new TabColors().display();
            }
        });
    }
}
trashgod
  • 203,806
  • 29
  • 246
  • 1,045
  • But that only sets the tab header color, not the content area...right? – mre Jan 06 '12 at 01:09
  • 2
    Right. As @camickr previously commented, the target component must be [opaque](http://java.sun.com/products/jfc/tsc/articles/painting/index.html#props). – trashgod Jan 06 '12 at 01:19
  • seems that opacity got me again! :[ – mre Jan 06 '12 at 01:21
  • @camickr gets the assist. I keep the opacity link handy, 'cause I _still_ forget. :-) – trashgod Jan 06 '12 at 01:28
  • 1
    I'm confused. I thought the "content area" was the area where the component you added to the tab is displayed. My suggestion was to make the component non-opaque so you could see the background of the content area? However, this didn't work so I deleted my comment. I don't understand how this suggestion affects the content area. Maybe someone could post a SSCCE (which should have been done with the original question anyway) to demonstrate the solution. – camickr Jan 06 '12 at 01:39
  • @camickr: Oops, I was thinking of `setOpaque(true)` to make `setBackground()` a viable choice for the content. – trashgod Jan 06 '12 at 02:07
  • Caveat: `setBackgroundAt()` seems to be ineffectual with Nimbus. – trashgod Jan 06 '12 at 02:16
  • 1
    Good example. It looks like the only solution is to set the background color of every component that is added as a tab. When using the Metal LAF on Windows the `setBackground()` method changes the background of all the tabs (but not the content area). Seems to me that this UI is a bit confusing. – camickr Jan 06 '12 at 06:14
  • Nimbus is more than confused than syntethica – mKorbel Jan 06 '12 at 08:19
0

You can also do the following:

for (int i = 0; i < this.getTabCount(); i++) {
    this.getComponentAt(i).setBackground(Color.DARK_GRAY);
}

or

for (int i = 0; i < this.getTabCount(); i++) {
            this.setBackgroundAt(i, Color.DARK_GRAY);
            this.getComponentAt(i).setBackground(Color.DARK_GRAY);
}

for tab and panel backgrounds

Jas
  • 385
  • 7
  • 22