3

JTabbedPane - tab border is being shown even after using BasicTabbedPaneUI. Please let me know how can I change such that no border is shown between the tabs? Is there any method to remove the border completely ?

enter image description here

Please Send me the code snippet. Thanks in advance.

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import javax.swing.BorderFactory;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTabbedPane;
import javax.swing.SwingUtilities;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import javax.swing.plaf.basic.BasicTabbedPaneUI;

public class TabbedPaneTest implements Runnable {

    @Override
    public void run() {
        final JTabbedPane pane = new JTabbedPane();
        pane.addTab("A", createTab(Color.RED));
        pane.addTab("B", createTab(Color.YELLOW));
        pane.addTab("C", createTab(Color.BLUE));

        pane.setUI(new BasicTabbedPaneUI() {
            @Override
            protected void paintContentBorder(Graphics g, int tabPlacement, int selectedIndex) {

            }
        });
        pane.setBackground(Color.WHITE);

        pane.addChangeListener(new ChangeListener() {

            @Override
            public void stateChanged(ChangeEvent arg0) {
                pane.setForegroundAt(pane.getSelectedIndex(), Color.GREEN);

            }
        });
        final JFrame frame = new JFrame();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.add(pane);
        frame.pack();
        //frame.setSize(500, 200);
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
    }

    private JPanel createTab(Color color) {
        JPanel p = new JPanel(new BorderLayout()) {
            private static final long serialVersionUID = 1L;

            @Override
            public Dimension getPreferredSize() {
                return new Dimension(400, 300);
            }
        };
        p.setBorder(BorderFactory.createLineBorder(color, 2));
        p.setBackground(Color.WHITE);
        return p;
    }

    public static void main(String[] args) throws Exception {
        SwingUtilities.invokeLater(new TabbedPaneTest());
    }
}
mKorbel
  • 109,525
  • 20
  • 134
  • 319
user3781572
  • 175
  • 1
  • 4
  • 1
    mybe will help you, you have to play with [calculateTabWidth and calculateTabHeight](http://stackoverflow.com/a/7056093/714968), but result, output shouldn't be nicer than, I think that there must be some borders, visually I'd be expecting it there, or directly to change [tabbedPane.contentBorderInsets in UIManager, for more info to check UIManager Defaults](https://tips4java.wordpress.com/2008/10/09/uimanager-defaults/) by @camickr – mKorbel Sep 23 '15 at 08:39

1 Answers1

5
// Remove the white line under a tab button
Insets insets = UIManager.getInsets("TabbedPane.contentBorderInsets");
insets.top = -1;
UIManager.put("TabbedPane.contentBorderInsets", insets);
cod
  • 51
  • 1
  • 2