S
Swetha
Hello
I have a Swing application and I'm trying to do the following:
I have a main JFrame in which JPanels are loaded based on events. Now,
I have a particular JPanel which in turn has 2 child JPanels. However,
both these panels are not created at the same time. I create one of the
2 initially and if the user clicks a button on the first JPanel, the
second is created. I am using the GridBagLayout.
My problem is this: When I create the first of 2 JPanels, it shows up
fine. When I click the button to create the second JPanel, the first
one changes its size and everything and appears totally weird. How can
I retain my first created JPanel's appearance and have my second JPanel
appear right too?
Following is code:
class MultiplePanels
{
JPanel pan;
JPanel mainPanel;
JPanel secPanel;
public void createPanel()
{
// Parent JPanel which is a component of the JFrame
pan = new JPanel();
pan.setLayout(new GridBagLayout());
pan.setPreferredSize(new Dimension(780, 500));
// 1st child JPanel
mainPanel = new JPanel();
mainPanel.setPreferredSize(780, 250);
mainPanel.setLayout(new GridBagLayout());
// Adding button to JPanel
JButton b = new JButton("Create new JPanel");
b.addActionListener(this);
// ........... adding other components to mainPanel
GridBagConstraints c = new GridBagConstraints();
c.gridy = 0;
c.anchor = GridBagConstraints.NORTH;
c.weighty = 1;
pan.add(mainPanel, c);
}
public void actionPerformed(ActionEvent e)
{
// 2nd child JPanel
secPanel = new JPanel();
secPanel.setPreferredSize(780, pan.getHeight() -
mainPanel.getHeight());
GridBagConstraints c = new GridBagConstraints();
c.gridy = 1;
c.anchor = GridBagConstraints.SOUTH;
pan.add(secPanel, c);
}
}
I have a Swing application and I'm trying to do the following:
I have a main JFrame in which JPanels are loaded based on events. Now,
I have a particular JPanel which in turn has 2 child JPanels. However,
both these panels are not created at the same time. I create one of the
2 initially and if the user clicks a button on the first JPanel, the
second is created. I am using the GridBagLayout.
My problem is this: When I create the first of 2 JPanels, it shows up
fine. When I click the button to create the second JPanel, the first
one changes its size and everything and appears totally weird. How can
I retain my first created JPanel's appearance and have my second JPanel
appear right too?
Following is code:
class MultiplePanels
{
JPanel pan;
JPanel mainPanel;
JPanel secPanel;
public void createPanel()
{
// Parent JPanel which is a component of the JFrame
pan = new JPanel();
pan.setLayout(new GridBagLayout());
pan.setPreferredSize(new Dimension(780, 500));
// 1st child JPanel
mainPanel = new JPanel();
mainPanel.setPreferredSize(780, 250);
mainPanel.setLayout(new GridBagLayout());
// Adding button to JPanel
JButton b = new JButton("Create new JPanel");
b.addActionListener(this);
// ........... adding other components to mainPanel
GridBagConstraints c = new GridBagConstraints();
c.gridy = 0;
c.anchor = GridBagConstraints.NORTH;
c.weighty = 1;
pan.add(mainPanel, c);
}
public void actionPerformed(ActionEvent e)
{
// 2nd child JPanel
secPanel = new JPanel();
secPanel.setPreferredSize(780, pan.getHeight() -
mainPanel.getHeight());
GridBagConstraints c = new GridBagConstraints();
c.gridy = 1;
c.anchor = GridBagConstraints.SOUTH;
pan.add(secPanel, c);
}
}