Zach Starnes
Zach Starnes

Reputation: 3198

JTabbedPane will not fill entire JPanel

I am adding a JTabbedPane to my JPanel and I can get it to stretch to the edges but the JTabbedPane is still leaving an ugly border around it. I thought the edges you just merge with the edges of the window. Does anyone know how to make it do that? Here is a picture of what I am seeing.

enter image description here

Here is my method that creates the layout:

public class MainFrame extends JFrame {
    private static final long serialVersionUID = 6112493789711533870L;
    private final int FRAME_WIDTH = 720;
    private final int FRAME_HEIGHT = 405;

    private JPanel mainLayout;
    GridBagConstraints c = new GridBagConstraints();

    public MainFrame(String t) {
        super(t);
        setSize(FRAME_WIDTH, FRAME_HEIGHT);
        add(createLayout());
    }

    public JPanel createLayout() {
        this.mainLayout = new JPanel(new GridBagLayout());
        JTabbedPane mainTabs = new JTabbedPane();
        mainTabs.setTabPlacement(JTabbedPane.LEFT);

        JComponent projectPanel = makeTextArea();
        mainTabs.addTab("Projects", projectPanel);

        JComponent tasksPanel = makeTextArea();
        mainTabs.addTab("Tasks", tasksPanel);

        JComponent optionsPanel = makeTextArea();
        mainTabs.addTab("Options", optionsPanel);

        c.fill = GridBagConstraints.BOTH;
        c.weightx = 1.0;
        c.weighty = 1.0;
        c.insets = new Insets(0,0,0,0);

        mainLayout.add(mainTabs, c);
        return mainLayout;
    }

    private JScrollPane makeTextArea() {
        JTextArea textArea = new JTextArea();
        textArea.setEditable(true);

        JScrollPane scrollTextArea = new JScrollPane(textArea);
        scrollTextArea.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);

        return scrollTextArea;
    }
} 

Upvotes: 1

Views: 1179

Answers (3)

Boann
Boann

Reputation: 50041

You can't really remove that border. It's part of the JTabbedPane UI. However, the problem I always have is the opposite: the Nimbus look and feel is, infuriatingly, the only one that does not have a border around tab pages. So, if you really want to avoid the border, you can use Nimbus by running this code once at application startup:

try {
    UIManager.setLookAndFeel("javax.swing.plaf.nimbus.NimbusLookAndFeel");
} catch (Throwable t) {
    throw new RuntimeException(t);
}

Otherwise, I would suggest adding an extra empty border around the components so the thin border doesn't look as ugly.

Edit: Instead of or in addition to the above, try removing the JScrollPane's border by adding this in makeTextArea():

scrollTextArea.setBorder(BorderFactory.createEmptyBorder());

This is what I see now: https://i.sstatic.net/afr8n.png

Upvotes: 1

MoYapro
MoYapro

Reputation: 119

You could add the JTabbedPane directly to your JFrame... why do you need that extra JPanel?

Upvotes: 0

Moritz Petersen
Moritz Petersen

Reputation: 13057

That depends on the used look and feel, try for example

    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());

to use the system look and feel. It still has the border, but it's getting better. If you want to get rid of the border at all, you need to change the look and feel of the component.

Upvotes: 0

Related Questions