Andrei Sh
Andrei Sh

Reputation: 113

How to set correctly sizes of a jpanels?

I have a two jpanels: jpanel1 and jpanel2 which must have correct minimal size according to their content. jpanel0 is the container for these two panels, it must be on the left side of a frame. And here is jpanel3 that should take the rest of the available space on the right side.

How to set the size of a jpanel to all available space?

My desired output: enter image description here

My current output: enter image description here

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;

import javax.swing.BorderFactory;
import javax.swing.BoxLayout;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTabbedPane;

public class Panels {

public static void main(String[] args) {

    JFrame myFrame = new JFrame();
    myFrame.setLayout(new BorderLayout());

    JTabbedPane jtp = new JTabbedPane();

    JPanel jPaneTab1 = new JPanel();
    jPaneTab1.setLayout(new FlowLayout(FlowLayout.LEFT));

    JPanel jpanel0 = new JPanel();
    jpanel0.setLayout(new BoxLayout(jpanel0, BoxLayout.Y_AXIS));
    jpanel0.setBorder(BorderFactory.createTitledBorder("jpanel0"));
    jpanel0.setBackground(Color.RED);


    JPanel jpanel1 = new JPanel();
    jpanel1.setLayout(new GridBagLayout());
    jpanel1.setBorder(BorderFactory.createTitledBorder("jpanel1"));
    GridBagConstraints gc = new GridBagConstraints();
    jpanel1.setBackground(Color.BLUE);

    JLabel jlabel1 = new JLabel("jlabel1");
    gc.gridx = 0;
    gc.gridy = 0;
    gc.anchor = GridBagConstraints.NORTHWEST;
    gc.insets = new Insets(0, 0, 0, 2);
    jpanel1.add(jlabel1, gc);
    JLabel jlabel2 = new JLabel("jlabel2");
    gc.gridx = 0;
    gc.gridy = 1;
    gc.anchor = GridBagConstraints.NORTHWEST;
    gc.insets = new Insets(0, 0, 0, 2);
    jpanel1.add(jlabel2, gc);


    JPanel jpanel2 = new JPanel();
    jpanel2.setLayout(new GridBagLayout());
    jpanel2.setBorder(BorderFactory.createTitledBorder("jpanel2"));
    GridBagConstraints gc2 = new GridBagConstraints();
    jpanel1.setBackground(Color.BLUE);

    JLabel jlabel3 = new JLabel("jlabel3");
    gc2.gridx = 0;
    gc2.gridy = 0;
    gc2.anchor = GridBagConstraints.NORTHWEST;
    gc2.insets = new Insets(0, 0, 0, 2);
    jpanel2.add(jlabel3, gc2);
    JLabel jlabel4 = new JLabel("jlabel4");
    gc2.gridx = 0;
    gc2.gridy = 1;
    gc2.anchor = GridBagConstraints.NORTHWEST;
    gc2.insets = new Insets(0, 0, 0, 2);
    jpanel2.add(jlabel4, gc2);






    JPanel jpanel3 = new JPanel();
    jpanel3.setBackground(Color.YELLOW);
    JLabel jlabel5 = new JLabel("jpanel3");
    jpanel3.add(jlabel5);

    jpanel0.add(jpanel1);
    jpanel0.add(jpanel2);
    jPaneTab1.add(jpanel0, BorderLayout.WEST);
    jPaneTab1.add(jpanel3, BorderLayout.CENTER);        

    JPanel jPaneTab2 = new JPanel();


    jtp.addTab("tab1", jPaneTab1);
    jtp.addTab("tab2", jPaneTab2);

    myFrame.add(jtp);       
    myFrame.setSize(800, 600);
    myFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    myFrame.setVisible(true);
}

}

Addition:

When I use BorderLayout for main(tab panel) I'm getting another problem: enter image description here

Upvotes: 0

Views: 257

Answers (2)

Jan Bodnar
Jan Bodnar

Reputation: 11657

You should not be using GridBagLayout, BorderLayout, or BoxLayout managers. These are outdated managers from the 90s.

For instance, when you do this:

gc.insets = new Insets(0, 0, 0, 2);

you are hardcoding pixel-width spaces between components, which will not work across the wide variety of today's screens.

Insted, one should choose either GroupLayout or MigLayout.

Here is a working example with the MigLayout manager. Notice how easy is to create the layout with this manager. (Four lines of code.) Also, we use logical pixels (lp) instead of physical pixels.

package com.zetcode;

import java.awt.EventQueue;
import javax.swing.BorderFactory;
import javax.swing.JFrame;
import static javax.swing.JFrame.EXIT_ON_CLOSE;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTabbedPane;
import net.miginfocom.swing.MigLayout;

public class PanelsEx extends JFrame {

    public PanelsEx() {

        initUI();
    }

    private void initUI() {

        JTabbedPane tabpane = new JTabbedPane();

        JPanel mainPanel = new JPanel();

        JPanel pnl1 = createPanel("Panel 1");
        JPanel pnl2 = createPanel("Panel 2");
        JPanel pnl3 = createPanel("Panel 3");        

        mainPanel.setLayout(new MigLayout("ins 10lp"));

        mainPanel.add(pnl1, "w 150lp, h 100lp, split 2, flowy, ay top");
        mainPanel.add(pnl2, "w 150lp, h 100lp");
        mainPanel.add(pnl3, "push, grow");

        tabpane.add("First", mainPanel);

        add(tabpane);

        pack();        

        setTitle("Panels");
        setLocationRelativeTo(null);
        setDefaultCloseOperation(EXIT_ON_CLOSE);

    }

    private JPanel createPanel(String text) {

        JLabel lbl = new JLabel(text);
        JPanel pnl = new JPanel();
        pnl.add(lbl);

        pnl.setBorder(BorderFactory.createEtchedBorder());

        return pnl;
    }

    public static void main(String[] args) {

        EventQueue.invokeLater(() -> {
            PanelsEx ex = new PanelsEx();
            ex.setVisible(true);
        });
    }
}

And here is a screenshot:

Screenshot of the code example

Upvotes: 2

camickr
camickr

Reputation: 324197

Layouts work easier when you break down the layout into logical panels and then nest panels with different layout managers.

For example, use a panel with a BorderLayout as the content for the tabbed pane.

Then you create a "ride side panel" and add it to this panel and a "center panel"

JPanel main = new JPanel( new BorderLayout() );
JPanel rightSide = new JPanel( ... );
JPanel center = new JPanel(...);
main.add(rightSide, BorderLayout.LINE_START);
main.add(center, BorderLayout.CENTER);

Then you set the layouts for the "rightSide" and "center" panels and add components to each of those panels.

Upvotes: 2

Related Questions