Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / February 2006

Tip: Looking for answers? Try searching our database.

Retain existing JPanel's appearance while creating new one

Thread view: 
Swetha - 12 Feb 2006 10:06 GMT
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);
   }
}
hiwa - 12 Feb 2006 10:51 GMT
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();
 }
}
Swetha - 12 Feb 2006 14:03 GMT
Thank you for the suggestion. But I figured out that I was mixing up
the Insets and that was creating the problem.

Swetha

> Please post a small demo code that is generally compilable, runnable
> and could reproduce your problem. See:
[quoted text clipped - 67 lines]
>   }
> }


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2009 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.