Results 1 to 2 of 2
  1. #1
    Ruddlesdin is offline Member
    Join Date
    Nov 2013
    Location
    Cambridge
    Posts
    6
    Rep Power
    0

    Lightbulb GridBagLayout is forcing a resize of parent JPanel when adding 4 JLabels

    I am trying work out why my parent JPanel (leftPanel3) is being resized when I put 4 JLabels inside it using GridBagLayout.
    This image is before the labels are added and this how I want the sizes to remain.
    GridBagLayout is forcing a resize of parent JPanel when adding 4 JLabels-clipboard01.jpg

    This image is after the 4 labels have been added and as you can see the JPanel leftPanel3 has resized. There appears to be plenty of space around the labels.
    GridBagLayout is forcing a resize of parent JPanel when adding 4 JLabels-clipboard02.jpg

    In my opinion there should be no need for this resize as I believe the 4 JLabels can fit inside the original space, that is the plan anyway. Any ideas?

    Code is below:
    Java Code:
    /*
     * To change this license header, choose License Headers in Project Properties.
     * To change this template file, choose Tools | Templates
     * and open the template in the editor.
     */
    package View;
    
    import java.awt.Color;
    import java.awt.Dimension;
    import java.awt.Font;
    import java.awt.GridBagConstraints;
    import java.awt.GridBagLayout;
    import java.awt.Insets;
    import javax.swing.BorderFactory;
    import javax.swing.ImageIcon;
    import javax.swing.JLabel;
    import javax.swing.JPanel;
    import javax.swing.SwingConstants;
    
    /**
     *
     * @author p_ruddlesdin
     */
    public class frontEnd extends JPanel{
        JPanel rootPanel1;
        JPanel rootPanel2;
        JPanel leftPanel3;
        JPanel leftPanel4;
        JPanel leftPanel5;
        JPanel leftPanel6;
        JPanel rightPanel7;
        JPanel rightPanel8;
        JPanel rightPanel9;
        JLabel label1;
        JLabel label2;
        JLabel label3;
        JLabel label4;
        
        GridBagLayout gbl1 = new GridBagLayout();
        GridBagLayout gbl2 = new GridBagLayout();
        GridBagLayout gbl3 = new GridBagLayout();
        
        GridBagConstraints gbc1 = new GridBagConstraints();
        GridBagConstraints gbc2 = new GridBagConstraints();
        GridBagConstraints gbc3 = new GridBagConstraints();
        
        //Begin constructor
        public frontEnd(){
            initComponents();
        } //End constructor
        
        public void initComponents()
        {
            // Set the layout to GridBagLayout
            this.setLayout(gbl1);
            
            // set grid constraints
            gbc1.fill = GridBagConstraints.BOTH;
            gbc1.gridx = 0;
            gbc1.gridy = 0;
            gbc1.weightx = 0.65;
            gbc1.weighty = 1.0;
            
            //create rootPanel1 // Left side of gui
            rootPanel1 = new JPanel();
            rootPanel1.setLayout(gbl1);
            gbc1.fill = GridBagConstraints.BOTH;
            add(rootPanel1,gbc1);
            
            //create rootPanel2 // Right side of gui
            rootPanel2 = new JPanel();
            gbc1.gridx = 1;
            gbc1.gridy = 0;
            gbc1.weightx = 0.35;
            gbc1.weighty = 1.0;
            rootPanel2.setLayout(gbl1);
            gbc1.fill = GridBagConstraints.BOTH;
            add(rootPanel2,gbc1);
            
            label1 = new JLabel();
            label2 = new JLabel();
            label3 = new JLabel();
            label4 = new JLabel();
            
            // create leftPanel3 // Title
            leftPanel3 = new JPanel(gbl2);
            
            gbc1.insets = new Insets(1,1,1,1);
            gbc1.fill = GridBagConstraints.BOTH;
            gbc1.gridx = 0;
            gbc1.gridy = 0;
            gbc1.weightx = 1.0;
            gbc1.weighty = 0.2;
    
            label1.setText("label1");
            label1.setPreferredSize(new Dimension(150,50));
            label1.setBorder(BorderFactory.createLineBorder(Color.GREEN));
            label2.setFont(new Font("Dialog", Font.BOLD, 12));
            label2.setText("label2");
            label2.setBorder(BorderFactory.createLineBorder(Color.YELLOW));
            label3.setFont(new Font("Dialog", Font.BOLD, 12));
            label3.setText("label3");
            label3.setBorder(BorderFactory.createLineBorder(Color.BLUE));
            label4.setFont(new Font("Tahoma", Font.BOLD, 48));
            label4.setText("label4");
            label4.setHorizontalAlignment(SwingConstants.CENTER);
            label4.setBorder(BorderFactory.createLineBorder(Color.RED));
            gbc2.insets = new Insets(1, 1, 1, 1);
            leftPanel3.setBorder(BorderFactory.createTitledBorder("leftPanel3"));
            leftPanel3.add(label1,gbc2);
            leftPanel3.add(label2,gbc2);
            leftPanel3.add(label3,gbc2);
            leftPanel3.add(label4,gbc2);
            
            rootPanel1.add(leftPanel3,gbc1);
            
            // create leftPanel4 // Message Area
            leftPanel4 = new JPanel();
            gbc1.insets = new Insets(2,2,1,1);
            gbc1.fill = GridBagConstraints.BOTH;
            gbc1.gridx = 0;
            gbc1.gridy = 1;
            gbc1.weightx = 1.0;
            gbc1.weighty = 0.5;
            leftPanel4.setBorder(BorderFactory.createTitledBorder("rightPanel4"));
            rootPanel1.add(leftPanel4,gbc1);
            
            // create leftPanel5 // Suppress Satus
            leftPanel5 = new JPanel();
            gbc1.insets = new Insets(2,2,1,1);
            gbc1.fill = GridBagConstraints.BOTH;
            gbc1.gridx = 0;
            gbc1.gridy = 2;
            gbc1.weightx = 1.0;
            gbc1.weighty = 0.1;
            leftPanel5.setBorder(BorderFactory.createTitledBorder("rightPanel5"));
            rootPanel1.add(leftPanel5,gbc1);
            
            // create leftPanel6 // Logs
            leftPanel6 = new JPanel();
            gbc1.insets = new Insets(2,2,2,1);
            gbc1.fill = GridBagConstraints.BOTH;
            gbc1.gridx = 0;
            gbc1.gridy = 3;
            gbc1.weightx = 1.0;
            gbc1.weighty = 0.2;
            leftPanel6.setBorder(BorderFactory.createTitledBorder("rightPanel6"));
            rootPanel1.add(leftPanel6,gbc1);
            
            // create rightPanel7 // Settings
            rightPanel7 = new JPanel();
            gbc1.insets = new Insets(2,1,1,2);
            gbc1.fill = GridBagConstraints.BOTH;
            gbc1.gridx = 0;
            gbc1.gridy = 0;
            gbc1.weightx = 1.0;
            gbc1.weighty = 0.5;
            rightPanel7.setBorder(BorderFactory.createTitledBorder("rightPanel7"));
            rootPanel2.add(rightPanel7,gbc1);
            
            // create rightPanel8 // Status
            rightPanel8 = new JPanel();
            gbc1.insets = new Insets(2,1,1,2);
            gbc1.fill = GridBagConstraints.BOTH;
            gbc1.gridx = 0;
            gbc1.gridy = 1;
            gbc1.weightx = 1.0;
            gbc1.weighty = 0.25;
            rightPanel8.setBorder(BorderFactory.createTitledBorder("rightPanel8"));
            rootPanel2.add(rightPanel8,gbc1);
            
            // create rightPanel9 // Options
            rightPanel9 = new JPanel();
            gbc1.insets = new Insets(2,1,2,2);
            gbc1.fill = GridBagConstraints.BOTH;
            gbc1.gridx = 0;
            gbc1.gridy = 2;
            gbc1.weightx = 1.0;
            gbc1.weighty = 0.25;
            rightPanel9.setBorder(BorderFactory.createTitledBorder("rightPanel9"));
            rootPanel2.add(rightPanel9,gbc1);
        }
    }

  2. #2
    cronnin is offline Member
    Join Date
    Jan 2017
    Posts
    38
    Rep Power
    0

    Default Re: GridBagLayout is forcing a resize of parent JPanel when adding 4 JLabels

    It's not just leftPanel3 that is resized, but also lp4, lp5, lp6. They are all inside rootPanel1. How are rootPanel1 and rootPanel2 managed?

Similar Threads

  1. Replies: 3
    Last Post: 09-09-2012, 09:47 AM
  2. GridBagLayout-forcing content left.
    By Mapisto in forum AWT / Swing
    Replies: 4
    Last Post: 08-16-2012, 09:40 PM
  3. GridBagLayout - forcing all components to stay on top
    By Serpenthelm in forum AWT / Swing
    Replies: 3
    Last Post: 01-05-2012, 06:52 PM
  4. gridbaglayout does not resize with the window
    By Marcoita_88 in forum AWT / Swing
    Replies: 5
    Last Post: 08-06-2011, 10:31 AM
  5. Replies: 1
    Last Post: 02-16-2008, 11:43 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •