1,105,197 Community Members

Frame and Multiple Panel ..Need Help

Member Avatar
cebubinary
Newbie Poster
18 posts since Jan 2008
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

hi im new to java and im learning about how to add/remove panels from another panel...


This is my main frame

import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class DynamicFrame extends JFrame{

   private JButton jb = new JButton("Remove");
   private JPanel jp = new JPanel();

    public DynamicFrame(){
            jp.add(jb);
            add(jp);
            setVisible(true);
            pack();

    jb.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent e){
            remove(jp);
            add( new DynamicPanel1().getD1());
            validate();
            pack();
            }
        });
    }
    public static void main(String a[]){
        new DynamicFrame();
    }
}

this is my panel 1

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

public class DynamicPanel1 extends JPanel{
    private JPanel jp = new JPanel();
    private JButton jb = new JButton("Dynamic Panel 2");
    private JLabel jl= new JLabel("Dynamic Panel 1");
    private JPanel center =new JPanel();
    public DynamicPanel1(){

       add(jl);
       add(jb);

        jb.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent e){
                //what to put?
            }
        });
    }
    public JPanel getD1(){
        return this;
    }
}

this is my panel2

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

public class DynamicPanel2 extends JPanel{



    private JPanel jp = new JPanel();
    private JLabel jl = new JLabel("Dyanamic 2");
    private JButton jb = new JButton("Dynamic Panel 1");
    public DynamicPanel2(){
       add(jl);
       add(jb);
        jb.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent e){
                                   //what to put?
            }
        });
    }
    public JPanel getD2(){
        return this;
    }
}
Member Avatar
BestJewSinceJC
Posting Maven
2,760 posts since Sep 2008
Reputation Points: 700 [?]
Q&As Helped to Solve: 366 [?]
Skill Endorsements: 17 [?]
 
0
 

Hi... use code tags as explained in the stick. And what is your question/problem?

Member Avatar
cebubinary
Newbie Poster
18 posts since Jan 2008
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

Hi... use code tags as explained in the stick. And what is your question/problem?

my problem is i want to add panel 2 and remove panel 1 when i click the button from panel 1
and vice versa.

Member Avatar
BestJewSinceJC
Posting Maven
2,760 posts since Sep 2008
Reputation Points: 700 [?]
Q&As Helped to Solve: 366 [?]
Skill Endorsements: 17 [?]
 
0
 

In the actionPerformed method put your code that removes the panel. The easiest way to remove a panel would be to keep a reference to the panel, i.e., have a JPanel class variable and set it to the JPanel that you are going to want to remove later. Then use the remove method found here: http://java.sun.com/j2se/1.5.0/docs/api/java/awt/Container.html#remove(java.awt.Component)


So you'd say jPanelItsStoredIn.remove(jPanelThatYouWantRemoved);

Alternatively, at least for BorderLayout, you can use the BorderLayout's methods to get the Component at a specific place in the BorderLayout. You can probably do this for all the Layout Managers. But keep in mind that no matter how you remove the Component, you'll have to keep track of some information to remember which Component you want to remove (if the Container [JPanel in this case] you're removing it from has more than one component in it). If it only has one Component in it, you can use the Container class's getComponent type methods to get it easily.

Member Avatar
cebubinary
Newbie Poster
18 posts since Jan 2008
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

In the actionPerformed method put your code that removes the panel. The easiest way to remove a panel would be to keep a reference to the panel, i.e., have a JPanel class variable and set it to the JPanel that you are going to want to remove later. Then use the remove method found here: http://java.sun.com/j2se/1.5.0/docs/api/java/awt/Container.html#remove(java.awt.Component)


So you'd say jPanelItsStoredIn.remove(jPanelThatYouWantRemoved);

Alternatively, at least for BorderLayout, you can use the BorderLayout's methods to get the Component at a specific place in the BorderLayout. You can probably do this for all the Layout Managers. But keep in mind that no matter how you remove the Component, you'll have to keep track of some information to remember which Component you want to remove (if the Container [JPanel in this case] you're removing it from has more than one component in it). If it only has one Component in it, you can use the Container class's getComponent type methods to get it easily.

got the removing.. but my main problem is how to add my panel 1 to the main frame if click the button from panel 2?

Member Avatar
BestJewSinceJC
Posting Maven
2,760 posts since Sep 2008
Reputation Points: 700 [?]
Q&As Helped to Solve: 366 [?]
Skill Endorsements: 17 [?]
 
0
 

You can pass the frames and panels around as necessary to your classes. For example, if you needed to use a JPanel inside of a class called MainFrame, your MainFrame constructor could look like the following:

public MainFrame(JPanel panel){
varToStoreThePanelIn = panel;
}

Then when you needed to add things to other containers, it would be easy to do using the container's add method.

Member Avatar
cebubinary
Newbie Poster
18 posts since Jan 2008
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

You can pass the frames and panels around as necessary to your classes. For example, if you needed to use a JPanel inside of a class called MainFrame, your MainFrame constructor could look like the following:

public MainFrame(JPanel panel){
varToStoreThePanelIn = panel;
}

Then when you needed to add things to other containers, it would be easy to do using the container's add method.

does this mean that in my jbutton
i will place there
new mainFrame(Passing the panel);
??

Member Avatar
BestJewSinceJC
Posting Maven
2,760 posts since Sep 2008
Reputation Points: 700 [?]
Q&As Helped to Solve: 366 [?]
Skill Endorsements: 17 [?]
 
0
 

No. But I'll help you more tomorrow & explain in more detail, I have to look at your problem more closely to give you more specific advice.

Member Avatar
cebubinary
Newbie Poster
18 posts since Jan 2008
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

No. But I'll help you more tomorrow & explain in more detail, I have to look at your problem more closely to give you more specific advice.

w0w, thanks....

Member Avatar
BestJewSinceJC
Posting Maven
2,760 posts since Sep 2008
Reputation Points: 700 [?]
Q&As Helped to Solve: 366 [?]
Skill Endorsements: 17 [?]
 
0
 

1.

add( new DynamicPanel1().getD1());

Calling the "getD1()" is redundant here because when you say new DynamicPanel1, you are creating a DynamicPanel1 Object. The getD1() method returns a DynamicPanel1 Object, so you see, you already did that anyway.

2.

"got the removing.. but my main problem is how to add my panel 1 to the main frame if click the button from panel 2?"

Think the problem out a little more thoroughly: You have three Objects - your main frame, your panel 1, and your panel 2. What you want to do is add panel 1 to the main frame from within panel 2's class. So if your panel 2 constructor takes two arguments: the main frame and the panel 1, then you can accomplish this. For example:

public class DynamicFrame extends JFrame{
      public DynamicFrame(){
         public void actionPerformed(ActionEvent e){
              add(new DynamicPanel1(this));
          }
      }

....

public class DynamicPanel1 extends JPanel{
 DynamicFrame myFrame = null;
      public DynamicPanel1(DynamicFrame frame){
       myFrame = frame;
      public void actionPerformed(ActionEvent e){
          //Is this where you want to create your DynamicPanel2?
          //I noticed you never created one... I'll show you how you'd
          //create it though:
          new DynamicPanel2(myFrame, this);
      }
      }

....

public class DynamicPanel2 extends JPanel{
     DynamicFrame myFrame;
     DynamicPanel1 myPanel1;
      public DynamicPanel2(DynamicFrame frame, DynamicPanel1 panel1){
       myFrame  = frame;
       myPanel1 = panel1;
     }
      public void actionPerformed(ActionEvent e){
       myFrame.add(myPanel1);
      }

It seems like you're making a legitimate effort to learn, so in this case, I think it is best to see how it is done so that you will understand in the future. But please note that you will not usually be given code on Daniweb, even if it is a few lines.... this is just one of those cases where "letting you figure it out on your own" isn't really advantageous.

Member Avatar
cebubinary
Newbie Poster
18 posts since Jan 2008
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

Ok.. thanks..

You
This article has been dead for over three months: Start a new discussion instead
Post:
Start New Discussion
Tags Related to this Article