2

I am trying to change the background color of my tabs in a JTabbedPane. I tried JTabbedPane.setBackgroudAt(0, Color.GRAY) and JTabbedPane.setBackgroud(Color.GRAY) and the foreground too, but nothing happens. I changed the background of the panel inside the tab, still nothing.

enter image description here

Edit 1: I'm using UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel"); if this can help with the solution

Edit 2: Link to a example, https://www.dropbox.com/s/0krn9vikvkq46mz/JavaApplication4.rar

Nathan
  • 8,093
  • 8
  • 50
  • 76
evandrobm
  • 445
  • 1
  • 7
  • 17
  • 2
    Possible duplicate of [How to change background color of JTabbedPane?](http://stackoverflow.com/questions/8752037/how-to-change-background-color-of-jtabbedpane) – trashgod Jul 04 '12 at 18:39
  • Which do you want to change, the tab or the tab's content? – trashgod Jul 04 '12 at 18:44
  • i want change the tab, this black part has to be gray like que content. Anda i read this quetion trashgod, but nothing there help me, so i create one myself :) – evandrobm Jul 04 '12 at 18:47
  • Sorry, I don't understand. I've suggested two options [below](http://stackoverflow.com/a/11334283/230513). – trashgod Jul 04 '12 at 19:09
  • sorry, i want to say, the white part need be gray like the gray in the bottom of the image! – evandrobm Jul 04 '12 at 19:22

4 Answers4

19

You can change the background color of the tab using setBackgroundAt(), as shown here.

tab

You can change the background color of the tab's content using setBackground(), as shown here. Typically you have to do this on the tab's content, as the enclosing JTabbedPane background color is obscured by the content.

content

If you still have trouble, please edit your question to include an sscce based on either example that exhibits the problem you envounter.

Addendum: Combining the methods is also possible:

combined

import java.awt.*;
import java.awt.event.*;
import java.util.Random;
import javax.swing.*;

public class JTabbedTest {

    private static JTabbedPane jtp;

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

            @Override
            public void run() {
                JFrame f = new JFrame();
                f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

                jtp = new JTabbedPane();
                jtp.setPreferredSize(new Dimension(320, 200));
                jtp.addTab("Reds", new ColorPanel(0, Color.RED));
                jtp.setBackgroundAt(0, Color.RED);
                jtp.addTab("Greens", new ColorPanel(1, Color.GREEN));
                jtp.setBackgroundAt(1, Color.GREEN);
                jtp.addTab("Blues", new ColorPanel(2, Color.BLUE));
                jtp.setBackgroundAt(2, Color.BLUE);

                f.add(jtp, BorderLayout.CENTER);
                f.pack();
                f.setVisible(true);
            }
        });
    }

    private static class ColorPanel extends JPanel implements ActionListener {

        private final Random rnd = new Random();
        private final Timer timer = new Timer(1000, this);
        private Color color;
        private Color original;
        private int mask;
        private JLabel label = new JLabel("Stackoverflow!");
        private int index;

        public ColorPanel(int index, Color color) {
            super(true);
            this.color = color;
            this.original = color;
            this.mask = color.getRGB();
            this.index = index;
            this.setBackground(color);
            label.setForeground(color);
            this.add(label);
            timer.start();
        }

        @Override
        public void actionPerformed(ActionEvent e) {
            color = new Color(rnd.nextInt() & mask);
            this.setBackground(color);
            jtp.setBackgroundAt(index, original);
        }
    }
}
trashgod
  • 203,806
  • 29
  • 246
  • 1,045
  • Sorry, your example was inaccessible to me; I've combined the two approaches above. – trashgod Jul 04 '12 at 19:48
  • well, i will try this, if not work i will find another place to put my exemple, thanks – evandrobm Jul 04 '12 at 21:40
  • @evandrobm you issue is used Win7 or Vista, use custom Look and Feel – mKorbel Jul 04 '12 at 21:50
  • i'm using win 7, and i use UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel"); – evandrobm Jul 05 '12 at 15:04
  • As mentioned by @evandrobm above, this does not work with WindowsLookAndFeel, tested with JRE x86 1.8.0_181 and JRE x64 1.8.0_172. It is noted in the API documentation that "It is up to the look and feel to honor this property, some may choose to ignore it.". – opeongo Jul 26 '18 at 16:01
  • 1
    Although it works in this example, note that better practice to instantiate the JTabbedPane on the EDT, not in a static section. – opeongo Jul 26 '18 at 16:21
  • @opeongo: Good point; see also this [Q&A](http://stackoverflow.com/q/7229226/230513) regarding `setPreferredSize()`. – trashgod Jul 27 '18 at 16:59
5
  • most of method for JTabbedPane is protected in the API, and not accesible from Swing methods

  • have to look for Custom XxxTabbedPaneUI, override these methods, and could be accesible from outside

  • correct way would be to implement Custom Look & Feel only, part of them override JTabbedPane

  • example for Custom XxxTabbedPaneUI

Community
  • 1
  • 1
mKorbel
  • 109,525
  • 20
  • 134
  • 319
  • i was avoiding this because it looks like more complicated, but i try and works very very well, perfect! you are very good man, i can even make more things that the expected, very very thank you man! – evandrobm Jul 05 '12 at 17:59
3

You should consider using a Look and Feel that does what you want, or failing that, changing the default UIManger settings for a JTabbedPane:

UIManager.put("TabbedPane.background", Color.GRAY);

If you opt for the latter, it must be done before you create your GUI.

For more on this, please see Rob Camick's excellent blog on the subject: UIManager Defaults.

Edit: I was wrong. It should be:

UIManager.put("TabbedPane.unselectedBackground", Color.GRAY);

But note that this may not work with every Look and Feel.

Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373
-3

It may be a problem that there is nothing added yet to the tab.

Try setting the content manager of the content panel to BorderLayout, adding a JPanel with BorderLayout. Center and then coloring that panel.

Bob Kaufman
  • 12,864
  • 16
  • 78
  • 107
DrEnquinox
  • 47
  • 5