Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to change background color of JTabbedPane?

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
like image 203
mre Avatar asked Jan 06 '12 00:01

mre


People also ask

How do I change the color of my JTabbedPane?

To set the background color of a single tab, use the setBackgroundAt() method. This gives an option to mention the index and the color. The index here is the index of the specific tab you want to color.

How do I change the background of a JPanel?

We can set a background color to JPanel by using the setBackground() method.

What is display by JTabbedPane?

A JTabbedPane contains a tab that can have a tool tip and a mnemonic, and it can display both text and an image. The shape of a tab and the way in which the selected tab is displayed varies by Look and Feel.


1 Answers

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();
            }
        });
    }
}
like image 57
trashgod Avatar answered Sep 20 '22 07:09

trashgod