5

How can I change the color of tab when is selected ? and its border ? in this case its Arbitros tab which is blue, how can i change this ? I'm using JTabbedPane inside JFrame I found this but its not working UIManager.put("TabbedPane.selected", Color.white); what am I doing wrong ?

public VentanaPrincipal_vista() {

    super("Ventana Principal");

    this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    this.setSize(1000, 500);
    this.setResizable(false);
    this.setUndecorated(true);
    this.setBackground(Color.BLUE);
    // add tabbedPane and tabs .

    tabs = new JTabbedPane();
    tabs.setBackground(new Color(83, 83, 83));
    tabs.setForeground(new Color(255, 255, 255));
    tabs.setBorder(null);
    UIManager.put("TabbedPane.selected", Color.white);
    this.add(tabs);

    menuBar = new BGMenuBar();
    menuBar.setColor(new Color(83, 83, 83));
    this.setJMenuBar(menuBar);

    menu = new JMenu("File");
    menu.setForeground(Color.white);
    menuBar.add(menu);

    close = new JMenuItem("Close");

    menu.add(close);
    close.addActionListener(this);
    close.setBackground(new Color(83, 83, 83));
    close.setForeground(new Color(255, 255, 255));

    op1 = new JMenuItem("option 1");
    op1.setBackground(new Color(83, 83, 83));
    op1.setForeground(new Color(255, 255, 255));

    menu.add(op1);

    this.setLocationRelativeTo(null);
    this.setVisible(true);

}// end of constructor

enter image description here

Frakcool
  • 10,915
  • 9
  • 50
  • 89
user3363537
  • 111
  • 1
  • 1
  • 8

5 Answers5

8

for me,the below solution worked, I just set the UImanager's TabbedPane.selected color property before creation of JTabbedPane object.

 UIManager.put("TabbedPane.selected", Color.red);

      tabbedPane = new JTabbedPane();
Dip686
  • 641
  • 6
  • 16
0

A comment to CodeyX1's answer: for me the uimanager unselected state works with these values (without the "Tab" word in the middle):

UIManager.put("TabbedPane.unselectedForeground", Color.xxx)
UIManager.put("TabbedPane.selectedBackground", Color.xxx)
afxentios
  • 2,502
  • 2
  • 21
  • 24
zakimatyi
  • 11
  • 4
0

I know it is old post but, I had also this problem, the button was focused, so this worked for me:

UIManager.put("TabbedPane.selected", Color.red); 
+
tabbedPane.setFocusable(false);
or 
UIManager.put("TabbedPane.focus", Color.red);
-1

Try this:

import java.awt.*;
import javax.swing.*;
import javax.swing.plaf.basic.BasicTabbedPaneUI;

public class TabHighlight extends JPanel
{
    private static final int MAX = 5;
    private JTabbedPane pane = new JTabbedPane();

    public TabHighlight()
    {
        for (int i = 0; i < MAX; i++)
        {
            Color color     = Color.black;   //Set default tab background to black
            pane.add("Tab " + String.valueOf(i), new TabContent(pane, i, color));

            pane.setBackgroundAt(i, color);
            pane.setForegroundAt(i, Color.white);
        }

        this.add(pane);
    }

    private static class TabContent extends JPanel
    {
        private TabContent(JTabbedPane panel, int i, Color color)
        {
            //Active and inactive tab coloring must be done
            //when rendering the CONTENT of the JTabbedPane object

            //Override these default settings to allow for active and inactive
            //tab background color changing

            panel.setUI(new BasicTabbedPaneUI()
            {
                @Override
                protected void installDefaults()
                {
                    super.installDefaults();

                    highlight       = Color.lightGray;
                    lightHighlight  = Color.white;

                    shadow          = Color.gray;
                    darkShadow      = Color.darkGray;

                    //Active tab background is white
                    //so set the focus color to white
                    //to hide the active tab focus
                    //marker seeing that we want the
                    //active tab backgound to be different
                    focus           = Color.black;
                }
            });

            //Set the active tab background to white
            UIManager.put("TabbedPane.selected", Color.gray);

            UIManager.put("TabbedPane.unselectedTabBackground", Color.black);

            //Remove borders
            UIManager.put("TabbedPane.contentBorderInsets", new Insets(0, 0, 0, 0));

            setOpaque(true);

            setBackground(color);
            setForeground(Color.white);

            add(new JLabel("Tab content " + String.valueOf(i)));
        }

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

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

    //Use this function here to change the look and feel to Java's default
    //If using a mac with OS X Yosemite or another recent Mac OS X release
    public static void initLookAndFeel()
    {
        try
        {
            UIManager.setLookAndFeel(
              UIManager.getCrossPlatformLookAndFeelClassName()
              );
        } 
        catch(UnsupportedLookAndFeelException e)
        {

        }
        catch(ClassNotFoundException e)
        {

        }
        catch(InstantiationException e)
        {

        }
        catch(IllegalAccessException e)
        {

        }
    }

    public static void main(String[] args)
    {
        EventQueue.invokeLater(new Runnable()
            {
                @Override
                public void run()
                {
                    initLookAndFeel();
                    new TabHighlight().display();
                }
            });
    }
}

Let's take take a look at this snippet of code that adds 5 tabs a program start up:

public TabHighlight()
{
    for (int i = 0; i < MAX; i++)
    {
       Color color     = Color.black;   //Set default tab background to black
       Color color2    = Color.white;   //Set default tab foreground to white

       pane.add("Tab " + String.valueOf(i), new TabContent(pane, i, color));

       pane.setBackgroundAt(i, color);
       pane.setForegroundAt(i, Color.white);
    }

    this.add(pane);
}

The first area that you are requesting help with your in program is in the "for" block. I will try to explain this in a way that you will understand.

The first two lines define the default background and foreground color for each inactive tab. You can change these to a color of your preference.

The third line adds a tab with the following attributes:

  1. A string defining the text that will be displayed on the tab
  2. A tab number, you don't have to include this if you don't want to
  3. Creates the second snippet of code which I will get to after this one

The fourth line applies the default background color

The final line applies the default foreground color. NOTE: these attributes cannot be changed with UIManager. I have tried a few alternative approaches but was unsuccessful. Darn, I wish there was an easier way to achieve this.

Now let's take a closer look at this snippit of code that defines the appearance of active and inactive tabs:

    private TabContent(JTabbedPane panel, int i, Color color)
    {
        //Active and inactive tab coloring must be done
        //when rendering the CONTENT of the JTabbedPane object

        //Override these default settings to allow for active and inactive
        //tab background color changing

        panel.setUI(new BasicTabbedPaneUI()
        {
            @Override
            protected void installDefaults()
            {
                super.installDefaults();

                highlight       = Color.lightGray;
                lightHighlight  = Color.white;

                shadow          = Color.gray;
                darkShadow      = Color.darkGray;

                //Active tab background is white
                //so set the focus color to white
                //to hide the active tab focus
                //marker seeing that we want the
                //active tab backgound to be different
                focus           = Color.black;
            }
        });

        //Set the active tab background to white
        UIManager.put("TabbedPane.selected", Color.gray);

        //Set the inactive tab background to black
        UIManager.put("TabbedPane.unselectedTabBackground", Color.black);

        //Remove borders
        UIManager.put("TabbedPane.contentBorderInsets"
                      , new Insets(0, 0, 0, 0));

        setOpaque(true);

        setBackground(color);
        setForeground(Color.white);

        add(new JLabel("Tab content " + String.valueOf(i)));
    }

In this part of the code, do the following:

  1. Override the UI by using this block of code below

    panel.setUI(new BasicTabbedPaneUI() { //place the code from step 2 here }

  2. Enter these properties of that section:

    highlight = Color.lightGray; lightHighlight = Color.white; shadow = Color.gray; darkShadow = Color.darkGray; focus = Color.black;

    Just like in the first snippet, you change these attributes as well. Here is a little tip: each tab has a little dotted box around the tab label, this marks the focus. If you want to hide this marker, simply set its color to match that of the active tab, which now has the focus.

  3. Use the UIManager to change the following attributes: UIManager.put("TabbedPane.selected", Color.gray); UIManager.put("TabbedPane.unselectedTabBackground", Color.black);

    This will allow you change active and inactive tab backgrounds during program runtime.

    You were using the UIManager which is the best way to achieve what you were trying
    to do. Using this bit of code here allows you to make the changes that you looking for, but you have do step 2 before any of these changes will be able to take effect. UIManager.put("TabbedPane.selectedForeground", Color.xxx) and UIManager.put("TabbedPane.unselectedTabForeground", Color.xxx) don't change the foreground color, the foreground color stays the same. UIManager.put("TabbedPane.selected", Color.xxx) will change the background color of the active tab and UIManager.put("TabbedPane.unselectedTabBackground") will change the background color of inactive tab.

Copy and paste these two snippets of code in to your file and change them as you need.

I would recommend that you copy the whole source code at the top and paste it into compiler and then run it first. This way you will be able see what this program does and when you go back to your original code that you are working with, you will know where to place these snippets of code.

Hope this helps.

If you are having trouble with this, post an SSCCE in your response so I can see where you are having trouble.

Thanks.

CodeyX1
  • 1
  • 3
-2

Please check the following method from java docs for a JTabbedPane

http://docs.oracle.com/javase/tutorial/uiswing/components/tabbedpane.html#eg

void setBackgroundAt(int, Color)

rahul pasricha
  • 931
  • 1
  • 14
  • 35
  • like i said, i want to know how to change the tab that is being selected, not the backgroun of all tabs in general or specific way – user3363537 May 02 '14 at 21:31