Answer the question
In order to leave comments, you need to log in
How to find out the size of JPanel?
When the getSize() method is called, it returns 0. How can I find out the size of the JPane?
Let me explain a little what I want to do! There is a frame with dimensions of 700 by 400, I install two panels on it and hang GridBagLayout () on the panel. So on the first panel panelButton there are two Jlabels and two Jtextfields, and the second panelGrid is located below and in this case it is filled with black color only for visibility. So I need to find out the size of panelGrid, for this to draw a grid in it, but in order to draw a grid, I need to know its dimensions, and when calling methods that return dimensions or width or length, all this leads to 0.
Here is the code:
public class LifeGame extends JFrame
{
public static final int DEF_W = 700;
public static final int DEF_H = 400;
private int standardX = 10;
private int standardY = 10;
private GridBagLayout gbl;
private GridBagConstraints c;
private JPanel panelButton;
private JPanel panelGrid;
private JTextField sizeGridX;
private JTextField sizeGridY;
private JLabel labelX;
private JLabel labelY;
public LifeGame()
{
setSize(DEF_W,DEF_H);
panelButton = new JPanel();
panelGrid = new JPanel();
gbl = new GridBagLayout();
c = new GridBagConstraints();
panelButton.setLayout(gbl);
panelGrid.setLayout(gbl);
labelX = new JLabel("SizeX");
c.gridx = GridBagConstraints.RELATIVE;
c.gridy = 0;
c.gridheight = 1;
c.gridwidth = 1;
c.anchor = GridBagConstraints.NORTHWEST;
//c.fill = GridBagConstraints.HORIZONTAL;
c.weightx = 0.0;
c.weighty = 0.0;
c.ipadx = 0;
c.ipad = 0;
c.insets = new Insets(5, 5, 0, 0);
panelButton.add(labelX, c);
sizeGridX = new JTextField(5);
c.gridx = GridBagConstraints.RELATIVE;
c.gridy = 0;
c.gridheight = 1;
c.gridwidth = 1;
c.anchor = GridBagConstraints.NORTHWEST;
//c.fill = GridBagConstraints.HORIZONTAL;
c.weightx = 0.0;
c.weighty = 0.0;
c.ipadx = 0;
c.ipad = 0;
c.insets = new Insets(5, 5, 0, 0);
panelButton.add(sizeGridX, c);
labelY = new JLabel("SizeY");
c.gridx = GridBagConstraints.RELATIVE;
c.gridy = 0;
c.gridheight = 1;
c.gridwidth = 1;
c.anchor = GridBagConstraints.NORTHWEST;
//c.fill = GridBagConstraints.HORIZONTAL;
c.weightx = 0.0;
c.weighty = 0.0;
c.ipadx = 0;
c.ipad = 0;
c.insets = new Insets(5, 5, 0, 0);
panelButton.add(labelY, c);
sizeGridY = new JTextField(5);
c.gridx = GridBagConstraints.RELATIVE;
c.gridy = 0;
c.gridheight = 1;
c.gridwidth = 1;
c.anchor = GridBagConstraints.NORTHWEST;
//c.fill = GridBagConstraints.HORIZONTAL;
c.weightx = 1.0;
c.weighty = 0.0;
c.ipadx = 0;
c.ipad = 0;
c.insets = new Insets(5, 5, 0, 0);
panelButton.add(sizeGridY, c);
c.gridx = 0;
c.gridy = 1;
c.gridheight = GridBagConstraints.REMAINDER;
c.gridwidth = GridBagConstraints.REMAINDER;
c.anchor = GridBagConstraints.NORTH;
c.fill = GridBagConstraints.BOTH;
c.weightx = 1.0;
c.weighty=1.0;
c.ipadx = 0;
c.ipad = 0;
c.insets = new Insets(5, 0, 0, 0);
//panelGrid.add(newButton());
panelGrid.setBackground(Color.BLACK);
panelButton.add(panelGrid, c);
add(panelButton);
//pack();
}
}
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question