Search code examples
javaswingjpanelgridbaglayout

JPanel: Can not get the width of the JPanel


Please have a look at the following code

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

public class TestForm extends JFrame
{
    private JLabel firstLabel, secondLabel;

    private JTextField firstTxt, secondTxt;

    private GridBagLayout mainLayout = new GridBagLayout();
    private GridBagConstraints mainCons = new GridBagConstraints();
    private JPanel centerPanel;

      public TestForm()
      {
          this.setLayout(mainLayout);
        //Declaring instance variables  
        firstLabel = new JLabel("First: ");
        secondLabel = new JLabel("Second: ");

        firstTxt = new JTextField(7);
        secondTxt = new JTextField(7);


        mainCons.anchor = GridBagConstraints.WEST;
        mainCons.weightx = 1;
        mainCons.gridy = 2;
        mainCons.gridx = 1;
        mainCons.insets = new Insets(15, 0, 0, 0);
        this.add(createCenterPanel(), mainCons);

        System.out.println("Center Panel Width: "+centerPanel.getWidth());        
        this.setTitle("The Test Form");
        this.pack();
        this.setLocationRelativeTo(null);
        this.setVisible(true);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);


    }

    private JPanel createCenterPanel()
    {
        centerPanel = new JPanel();

        GridBagLayout gbl = new GridBagLayout();
        GridBagConstraints gbc = new GridBagConstraints();

        centerPanel.setLayout(gbl);

        gbc.gridx = 1;
        gbc.gridy = 1;
        gbc.fill = GridBagConstraints.BOTH;
        gbc.insets = new Insets(15,0,0,0);
        centerPanel.add(firstLabel,gbc);

        gbc.gridx = 2;
        gbc.gridy = 1;
        gbc.fill = GridBagConstraints.BOTH;
        gbc.insets = new Insets(15,0,0,0);
        centerPanel.add(firstTxt,gbc);

        gbc.gridx = 3;
        gbc.gridy = 1;
        gbc.fill = GridBagConstraints.BOTH;
        gbc.insets = new Insets(15,10,0,0);
        centerPanel.add(secondLabel,gbc);

        gbc.gridx = 4;
        gbc.gridy = 1;
        gbc.fill = GridBagConstraints.BOTH;
        gbc.insets = new Insets(15,-10,0,0);
        centerPanel.add(secondTxt,gbc);


        centerPanel.setBorder(BorderFactory.createTitledBorder("The Testing Form"));
        centerPanel.setPreferredSize(centerPanel.getPreferredSize());
        centerPanel.validate();

        System.out.println("Width is: "+centerPanel.getWidth());
        System.out.println("Width is: "+centerPanel.getHeight());

        return centerPanel;

    }


    public static void main(String[]args)
    {
        try
        {
            UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
            new TestForm();
        }
        catch(Exception e)
        {
            e.printStackTrace();
        }
    }
}

I am trying to get the width of the centerPanel in 2 places. But in both cases, I get 0 as the answer! I must get the width and height somehow because the southPanel (not included here) will use those values, with some reduce to the height. Please help!


Solution

  • The width is 0 before pack() or setSize() is called. You can get the width and use it after pack() call

    or

    define your own LayoutManager which use the width for the height of another layout part (southPanel)