How to change background color of JTabbedPane?
Asked Answered
S

2

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
Saporous answered 6/1, 2012 at 0:49 Comment(3)
Do you meant the color of the tab itselfFilomena
I mean the tab header (i.e. where the title goes) and the content area.Saporous
I don't see a setContentAreaBackground() method so it looks like you will need to create a custom UI to do this.Ginny
F
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();
            }
        });
    }
}
Filomena answered 6/1, 2012 at 1:6 Comment(9)
But that only sets the tab header color, not the content area...right?Saporous
Right. As @Ginny previously commented, the target component must be opaque.Filomena
seems that opacity got me again! :[Saporous
@Ginny gets the assist. I keep the opacity link handy, 'cause I still forget. :-)Filomena
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.Ginny
@camickr: Oops, I was thinking of setOpaque(true) to make setBackground() a viable choice for the content.Filomena
Caveat: setBackgroundAt() seems to be ineffectual with Nimbus.Filomena
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.Ginny
Nimbus is more than confused than syntethicaBroomfield
M
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

Mertz answered 10/6, 2016 at 16:56 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.