문제

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!

도움이 되었습니까?

해결책

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;
               }
            } );
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top