Search code examples
javaswinglook-and-feel

Swing: Custom LaF for JTabbedPane component subclass


How can I subclass a JTabbedPane and give each instance of my subclass a Look-and-Feel that is different from the default LaF? I still want access to the default JTabbedPanel, so simply overriding the global LaF is not an option.

Thanks.


Solution

  • You can manually set the specific UI implementation of the JTabbedPane (and most other JComponents) by calling the setUI(TabbedPaneUI ui) method.

    For example to set a specific JTabbedPane to use the Metal Look and Feel use:

    JTabbedPane fooPane = new JTabbedPane();
    fooPane.setUI(MetalTabbedPaneUI.createUI(fooPane));
    

    When creating a subclass (like you do) of JTabbedPane you will need to override the method updateUI() like this:

    public void updateUI() {
        setUI(MetalTabbedPaneUI.createUI(this));
    }