Retain existing JPanel's appearance while creating new one

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);
}
}
 
H

hiwa

Please post a small demo code that is generally compilable, runnable
and could reproduce your problem. See:
http://homepage1.nifty.com/algafield/sscce.html

Your code has no problem so far, it seems:
---------------------------------------------------------------------------------
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

class MultiplePanels implements ActionListener{
JPanel pan;
JPanel mainPanel;
JPanel secPanel;
JButton b;
GridBagConstraints c;

public MultiplePanels(){
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Container con = frame.getContentPane();

createPanel();

con.add(pan, BorderLayout.CENTER);
con.add(b, BorderLayout.SOUTH);
frame.pack();
frame.setVisible(true);
}

public void createPanel(){
pan = new JPanel();
pan.setLayout(new GridBagLayout());
pan.setPreferredSize(new Dimension(780, 500));

// 1st child JPanel
mainPanel = new JPanel();
mainPanel.setBackground(Color.yellow);
mainPanel.setPreferredSize(new Dimension(780, 250));
mainPanel.setLayout(new GridBagLayout());

b = new JButton("Create new JPanel");
b.addActionListener(this);
// ........... adding other components to mainPanel

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.setBackground(Color.pink);
secPanel.setPreferredSize
(new Dimension(780, pan.getHeight() - mainPanel.getHeight()));
c.gridy = 1;
c.anchor = GridBagConstraints.SOUTH;

pan.add(secPanel, c);
pan.revalidate();

b.setEnabled(false);
}

public static void main(String[] args){
new MultiplePanels();
}
}
 
S

Swetha

Thank you for the suggestion. But I figured out that I was mixing up
the Insets and that was creating the problem.

Swetha
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top