JTabbedPane - set default border around tabs..?

ClarkeyBoy picture ClarkeyBoy · Jun 26, 2010 · Viewed 11.5k times · Source

I am using a JTabbedPane in my application. I have added two tabs which are instances of a custom class "ContentPanel". This extends JPanel and sets the background, border etc etc. Basically it means I dont have to set the properties of each JPanel I want to apply this colour scheme to. I notice that not only does their border appear but another border (which, I think, is blue - at least on my screen) appears around this border, connected to the tab "selectors" themselves (i.e. the buttons you click on to get the appropriate view). I would like to change this border as it just looks odd against a gold / brown colour scheme. Does anyone have any idea how to do this? I have tried JTabbedPane.setBorder(Border b) but that doesnt work. That simply sets a border around the entire thing, including the tab selectors.. not what I want.

Any help with this would be greatly appreciated.

Answer

akf picture akf · Jun 26, 2010

These colors are defined in the Look and Feel. If you look at the code for BasicTabbedPaneUI, you will notice that installDefaults() sets a bunch of protected Color instance variables. The keys they are defined against in the L&F are also available here.

protected void installDefaults() {
    LookAndFeel.installColorsAndFont(tabPane, "TabbedPane.background",
                                "TabbedPane.foreground", "TabbedPane.font");     
    highlight = UIManager.getColor("TabbedPane.light");
    lightHighlight = UIManager.getColor("TabbedPane.highlight");
    shadow = UIManager.getColor("TabbedPane.shadow");
    darkShadow = UIManager.getColor("TabbedPane.darkShadow");
    //...
    // a lot more stuff
    //...
}

If you do not want to go as far as define your own L&F, you have the ability to set a custom UI delegate on your tabbed pane:

myTabbedPane.setUI(new BasicTabbedPaneUI() {
   @Override
   protected void installDefaults() {
       super.installDefaults();
       highlight = Color.pink;
       lightHighlight = Color.green;
       shadow = Color.red;
       darkShadow = Color.cyan;
       focus = Color.yellow;
   }
});

you may of course want to change those color settings. As set, you will see which vars are used where.