0

I'm trying to get the JTabbedPane automatically expanding over the parent JPanel.

When I put everything in the Main class, it works:

enter image description here

Main:

public class Main extends JFrame {

    public Main() {
        JTabbedPane tpane = new JTabbedPane();
        JPanel panel = new JPanel();
        panel.add(new JButton("Button 1"));
        tpane.addTab("Tab1", panel);

        JPanel panel2 = new JPanel();
        panel2.add(new JButton("Button 2"));
        tpane.addTab("Tab2", panel2);

        this.setSize(500, 500);
        this.setLocationRelativeTo(null);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.add(tpane);
        this.setVisible(true);
    }

    public static void main(String[] args) {
        Main m = new Main();
    }
}

But when I put it into another class, it won't work anymore:

enter image description here

Main:

public class Main extends JFrame {

    View view = new View();

    public Main() {
        this.setSize(500, 500);
        this.setLocationRelativeTo(null);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.add(view, BorderLayout.CENTER); // BorderLayout
        this.setVisible(true);
    }

    public static void main(String[] args) {
        Main m = new Main();
    }
}

View:

public class View extends JPanel {

    public View() {
        JTabbedPane tpane = new JTabbedPane();
        JPanel panel = new JPanel();
        panel.add(new JButton("Button 1"));
        tpane.addTab("Tab1", panel);

        JPanel panel2 = new JPanel();
        panel2.add(new JButton("Button 2"));
        tpane.addTab("Tab2", panel2);

        this.add(tpane, BorderLayout.CENTER); // BorderLayout
    }
}
Evgenij Reznik
  • 17,916
  • 39
  • 104
  • 181

1 Answers1

1

The frame has a border layout, the panel has a flow layout.

  • A component added to a border layout with no constraint ends up in the CENTER & will be stretched to the available height and width.
  • A component added to a flow layout will maintain its natural size.

More generally, don't set the size of a top level container. It is better to call pack() which will make the TLC the exact size needed to accommodate the components within. To add white space to a GUI, use layout constraints (not especially relevant when the layout only has a single component), or borders. See this answer for a working example.


Edit

I set a BorderLayout to both, Main and View. But the result remained the same.

This is the result of changing the layout of the View as seen here.

enter image description here

import java.awt.BorderLayout;
import javax.swing.*;

public class Main extends JFrame {

    View view = new View();

    public Main() {
        this.setSize(500, 500);
        this.setLocationRelativeTo(null);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.add(view);
        this.setVisible(true);
    }

    public static void main(String[] args) {
        Main m = new Main();
    }
}

class View extends JPanel {

    public View() {
        super(new BorderLayout()); // Just 1 line difference!
        JTabbedPane tpane = new JTabbedPane();
        JPanel panel = new JPanel();
        panel.add(new JButton("Button 1"));
        tpane.addTab("Tab1", panel);

        JPanel panel2 = new JPanel();
        panel2.add(new JButton("Button 2"));
        tpane.addTab("Tab2", panel2);

        this.add(tpane);
    }
}
Community
  • 1
  • 1
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
  • I set a `BorderLayout` to both, `Main` and `View`. But the result remained the same. Please see my update. – Evgenij Reznik Mar 12 '17 at 12:45
  • *"But the result remained the same."* Not here. Please see the update to my answer. – Andrew Thompson Mar 12 '17 at 13:12
  • Now it works, thank you. But why it doesn't work, when setting a `BorderLayout` like in my example? – Evgenij Reznik Mar 12 '17 at 13:27
  • Change (in the 2nd example edit) `this.add(tpane, BorderLayout.CENTER); // BorderLayout` to (not tested) `System.out.println("Layout is: " + this.getLayout()); this.add(tpane, BorderLayout.CENTER); // BorderLayout`. If you cannot figure it out from the result, get back to me.. – Andrew Thompson Mar 12 '17 at 14:19