2

How to remove the indents around icon tabs in JTabbedPane? i.e. what would this icon was on the full size of the tabs?

silverhawk
  • 569
  • 1
  • 6
  • 14

1 Answers1

3

You can try next trick with getTabInsets() method of BasicTabbedPaneUI:

import java.awt.Insets;

import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTabbedPane;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.plaf.basic.BasicTabbedPaneUI;

public class TestFrame extends JFrame {

    public TestFrame() {
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        init();
        pack();
        setVisible(true);
    }

    private void init() {
        try {
            UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
        } catch (ClassNotFoundException | InstantiationException
                | IllegalAccessException | UnsupportedLookAndFeelException e) {
            e.printStackTrace();
        }
        JTabbedPane pane = new JTabbedPane(JTabbedPane.BOTTOM);
        pane.addTab("", new ImageIcon(TestFrame.class.getResource("1111.png")), new JLabel("lbl"));
        pane.addTab("test2", new JLabel("lbl2"));
        pane.setFocusable(false);
        pane.setUI(new BasicTabbedPaneUI() {
            @Override
            protected Insets getTabInsets(int tabPlacement, int tabIndex) {
                return new Insets(0, 0, 0, 0);
            }
        });
        add(pane);
    }


    public static void main(String... strings) {
        new TestFrame();
    }

}

enter image description here

Also try calculateTabWidth() and calculateTabHeight().

alex2410
  • 10,904
  • 3
  • 25
  • 41
  • For what this? calculateTabWidth() and calculateTabHeight() – silverhawk Jun 18 '14 at 11:25
  • +1 right direction, but there is required [to override more methods](http://stackoverflow.com/a/7056093/714968), just awful API, most of ..., top three – mKorbel Jun 18 '14 at 11:27
  • @silverhawk, You can see some gap around image, these methods can minimize it. Also read link posted by mKorbel. – alex2410 Jun 18 '14 at 11:29