Question

The border of my custom tab component doesn't cover border of tab itself.

Instead my border is only around text of tab. To see what is wrong please run this demo:

public class TabBoundsDemo extends JTabbedPane {
    Border lineBorder = BorderFactory.createLineBorder(Color.RED, 2);

    public TabBoundsDemo() {
        super(TOP, WRAP_TAB_LAYOUT);

        for (int i = 0; i < 16; i++) {
            TabComponent tab = new TabComponent("tab " + i);
            super.addTab(null, new JLabel("label" + i));
            super.setTabComponentAt(i, tab);
        }

        super.setPreferredSize(new Dimension(300, 300));

    }

    class TabComponent extends JLabel {

        public TabComponent(String text) {
            super(text);
            super.setBorder(lineBorder);
            super.addMouseListener(new MouseAdapter() {
                @Override
                public void mousePressed(MouseEvent e) {
                    int tabIndex = getTabIndexAtMouseEvent(e);
                    System.out.println("tabIndex=" + tabIndex);

                }
            });
        }
    }

    public static void main(String[] args) {
        JFrame frame = new JFrame();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.add(new TabBoundsDemo());
        frame.pack();
        frame.setVisible(true);
    }
}

So I want my red border to be on place of standard tab border. For now it's only around title.

Thank you!

Was it helpful?

Solution

Add the following code in the constructor just below "super(TOP, WRAP_TAB_LAYOUT);" and let us know if that helps.

 setUI( new BasicTabbedPaneUI() {
               @Override
               protected void installDefaults() {
                   super.installDefaults();
                   highlight = Color.pink;
                   lightHighlight = Color.green;
                   shadow = Color.red;
                   darkShadow = Color.cyan;
                   focus = Color.yellow;
               }
            } );
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top