Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make Java Swing components fill available space?

Tags:

java

swing

I cannot seem to get my Java Swing components to work together correctly.

What I want to do, is have a JPanel fill ALL the space available inside a JTabbedPane. At the moment, my setup is as follows:

public class Gui extends JFrame {
    private final EventBus eventBus = EventBus.getInstance();
    private final ToolkitUtil toolkitUtil;
    private final Menu menu;
    private final InfoBar infoBar;
    private final JTabbedPane pane;

    ...

    private void buildLayout() {
        GridBagConstraints gbc = new GridBagConstraints();

        setJMenuBar(menu);

        add(pane, BorderLayout.CENTER);
        add(infoBar, BorderLayout.SOUTH);

        pane.addTab("Plugins", new PluginPanel());
    }
}

public class PluginPanel extends JPanel {
    private final JPanel modelPanel;
    private final JPanel editorPanel;

    public PluginPanel() {
        setLayout(new GridBagLayout());

        modelPanel = new JPanel(new GridBagLayout());
        editorPanel = new JPanel(new GridBagLayout());

        buildLayout();
    }

    private void buildLayout() {
        GridBagConstraints gbc = new GridBagConstraints();

        modelPanel.setBorder(BorderFactory.createTitledBorder("Models"));
        editorPanel.setBorder(BorderFactory.createTitledBorder("Editors"));

        gbc.gridx = 0;
        gbc.fill = GridBagConstraints.BOTH;

        modelPanel.add(new JLabel("test label"), gbc);
        add(modelPanel, gbc);

        gbc.gridx = 1;
        add(editorPanel, gbc);
    }
}

This creates a windows that is my desired size (dynamically proportional to the screen size, not included in above code). The tab panel that is placed in the center is expanded to fill all the space required, which is exactly what I want. But, the panels I add inside the tab panel are only as big as their content. If I add labels or anything, it only grows as big as the components. I want them to always be expanded to fill the tab panel.

like image 695
Nico Huysamen Avatar asked Apr 09 '11 09:04

Nico Huysamen


People also ask

Can we place AWT components in Swing container?

All these components are lightweight components. This class provides some common functionality like pluggable look and feel, support for accessibility, drag and drop, layout, etc. It inherits Component and Container of AWT. It cannot be contained within other containers.

What is Setlayout in Java Swing?

Java (Swing/AWT) uses something called LayoutManager s to place UI components on the screen. These LayoutManagers are responsible to render component such as TextField, CheckBox, etc in a predefined manner on your Window. For example: A LayoutManager called FlowLayout simply places components one after the other.


2 Answers

The easiest way is to use a BorderLayout and put the component in the CENTER position.

like image 59
Michael Borgwardt Avatar answered Sep 23 '22 12:09

Michael Borgwardt


Try setting the weights of the GridBagConstraints to non-zero values:

gbc.weightx = gbc.weighty = 1.0;
like image 44
perp Avatar answered Sep 21 '22 12:09

perp