Why is one of the panel in GridBagLayout expanding when i add a JLabel?

165 views Asked by At

I am trying to create a GUI with a gridbaglayout.

Below is the GUI i created with a gridbaglayout which is perfectly sized.

http://postimg.org/image/azjsw5xvd/

However, when i try to add a JLabel in the "Functions" panel, my "Functions" panel automatically expands and changes my GUI.

See below with automatically expanded Panel.

http://postimg.org/image/xu1h21lk1/

How do i lock all panels from changing their size and keeping it in this panel ratios?

Below is the source code i used to to align my GUI using gridbaglayout. I wish to make sure all panels maintain the same panel ratios when i resize my frame or add components into my sub-panels.

    frame = new JFrame("Cocoon");
    frame.setExtendedState(JFrame.MAXIMIZED_BOTH);

    background = new JPanel();
    background.setLayout(new GridBagLayout());
    GridBagConstraints gbc = new GridBagConstraints();
    frame.getContentPane().add(background, BorderLayout.CENTER);

    filePanel = new FilesPanel();
    gbc.gridx = 0;
    gbc.gridy = 0;
    gbc.gridwidth = 2;
    gbc.gridheight = 10;
    gbc.weightx = 0.2;
    gbc.weighty = 1.0;
    gbc.fill = GridBagConstraints.BOTH;
    gbc.insets = new Insets(10, 10, 10, 10);
    background.add(filePanel, gbc);

    beforePanel = new BeforePanel();
    afterPanel = new AfterPanel();

    gbc.gridx = 2;
    gbc.gridy = 0;
    gbc.gridwidth = 7;
    gbc.gridheight = 8;
    gbc.weightx = 0.5;
    gbc.weighty = 0.8;
    codes = new JSplitPane(JSplitPane.VERTICAL_SPLIT, beforePanel, afterPanel);
    codes.setOneTouchExpandable(true);
    codes.setResizeWeight(0.5);
    background.add(codes, gbc);

    feedBackPanel = new FeedbackPanel();
    gbc.gridx = 2;
    gbc.gridy = 8;
    gbc.gridwidth = 5;
    gbc.gridheight = 2;
    gbc.weightx = 0.5;
    gbc.weighty = 0.2;
    background.add(feedBackPanel, gbc);

    functionsPanel = new FunctionsPanel();
    gbc.gridx = 7;
    gbc.gridy = 0;
    gbc.gridwidth = 3;
    gbc.gridheight = 10;
    gbc.weightx = 0.3;
    gbc.weighty = 1.0;
    background.add(functionsPanel, gbc);

    frame.add(background);
    frame.setDefaultCloseOperation(frame.EXIT_ON_CLOSE);
    frame.validate();
    frame.pack();
    frame.setVisible(true);

Thank you.

1

There are 1 answers

0
jpyams On

Check out this answer. Basically there is no easy setting to get GridBagLayout from resizing the layout based on the content.