Help with setting background color for tabs

Hello all,

I’m a begginer in docking frames (just started yesterday), and was able to do useful stuff with it already. It is really a very good piece of software.
But there is something I was not able to do: setting a different color for the background of the tab of specific docks. I’m using CControl with default content area.
I tried to do this:

DefaultSingleCDockable dock = new DefaultSingleCDockable("someid", "Title", someComponent);
dock.getColorMap().setColor(ColorMap.COLOR_KEY_TAB_BACKGROUND, Color.RED);

but it didn’t work.
Setting other color keys did work, like COLOR_KEY_TITLE_BACKGROUND, our COLOR_KEY_MINIMIZED_BUTTON_BACKGROUND. But with the tab background I had no success.

Does anybody have an idea on how to set a different color for the tab of a specific dockable when it is stacked?

thanks in advance,

Fabio

Which theme are you using? Unfortunately the background color of tabs cannot be changed in the default theme (I never figured out how to set the color on a JTabbedPane).

You can change the theme with CControl.setTheme(ThemeMap. “the theme you like” )

I’m using the EclipseTheme.

Setting the color of a tab in a JTabbedPane is not straightforward. One need to customize the LaF in order to do that, like this:

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.EventQueue;
import java.awt.Graphics;
import javax.swing.JFrame;
import javax.swing.JTabbedPane;
import javax.swing.JTextArea;
import javax.swing.plaf.metal.MetalTabbedPaneUI;

public class TabbedPaneTest extends JFrame {

    private static final Color tab2BGColor = Color.RED;
    private static final Color tab2BGColorSelected = Color.ORANGE;

    public TabbedPaneTest() {
        init();
    }

    private void init() {
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        
        setLayout(new BorderLayout());
        JTabbedPane tabbedPane = new JTabbedPane();
        tabbedPane.addTab("Tab0", new JTextArea("Tab 0"));
        tabbedPane.addTab("Tab1", new JTextArea("Tab 1"));
        tabbedPane.addTab("Tab2", new JTextArea("Tab 2"));
        tabbedPane.addTab("Tab3", new JTextArea("Tab 3"));
        
        tabbedPane.setUI(new CustomUI());
        getContentPane().add(tabbedPane, BorderLayout.CENTER);
        
        setTitle("JTabbedPane color test");
        setBounds(200, 200, 300, 250);
    }

    public static void main(String[] args) {
        TabbedPaneTest main = new TabbedPaneTest();
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                main.setVisible(true);
            }
        });
    }

    private static class CustomUI extends MetalTabbedPaneUI {

        @Override
        protected void paintTabBackground(Graphics g, int tabPlacement,
                int tabIndex, int x, int y, int w, int h, boolean isSelected) {
            if (tabIndex == 2) {
                g.setColor(isSelected ? tab2BGColorSelected : tab2BGColor);
                g.fillRect(x, y, w, h);
            } else {
                super.paintTabBackground(g, tabPlacement, tabIndex, x, y, w, h, isSelected);
            }
        }

    }

}

Nevermind, it worked with eclipse theme. My mistake while setting the color.

Thank you very much.

Sorry for the late respone, I was not online.

Well… I don’t have to add anything, you already solved your problem.

P.S. yeah… the UI hack. Unfortunatelly does not work well if people use different look and feels :frowning: