943,584 Members | Top Members by Rank

Ad:
  • Java Discussion Thread
  • Unsolved
  • Views: 5774
  • Java RSS
You are currently viewing page 1 of this multi-page discussion thread
Jun 30th, 2009
0

Frame and Multiple Panel ..Need Help

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


This is my main frame

java Syntax (Toggle Plain Text)
  1. import java.awt.*;
  2. import javax.swing.*;
  3. import java.awt.event.*;
  4. public class DynamicFrame extends JFrame{
  5.  
  6. private JButton jb = new JButton("Remove");
  7. private JPanel jp = new JPanel();
  8.  
  9. public DynamicFrame(){
  10. jp.add(jb);
  11. add(jp);
  12. setVisible(true);
  13. pack();
  14.  
  15. jb.addActionListener(new ActionListener(){
  16. public void actionPerformed(ActionEvent e){
  17. remove(jp);
  18. add( new DynamicPanel1().getD1());
  19. validate();
  20. pack();
  21. }
  22. });
  23. }
  24. public static void main(String a[]){
  25. new DynamicFrame();
  26. }
  27. }

this is my panel 1

java Syntax (Toggle Plain Text)
  1. import java.awt.*;
  2. import javax.swing.*;
  3. import java.awt.event.*;
  4.  
  5. public class DynamicPanel1 extends JPanel{
  6. private JPanel jp = new JPanel();
  7. private JButton jb = new JButton("Dynamic Panel 2");
  8. private JLabel jl= new JLabel("Dynamic Panel 1");
  9. private JPanel center =new JPanel();
  10. public DynamicPanel1(){
  11.  
  12. add(jl);
  13. add(jb);
  14.  
  15. jb.addActionListener(new ActionListener(){
  16. public void actionPerformed(ActionEvent e){
  17. //what to put?
  18. }
  19. });
  20. }
  21. public JPanel getD1(){
  22. return this;
  23. }
  24. }
this is my panel2
java Syntax (Toggle Plain Text)
  1. import java.awt.*;
  2. import javax.swing.*;
  3. import java.awt.event.*;
  4.  
  5. public class DynamicPanel2 extends JPanel{
  6.  
  7.  
  8.  
  9. private JPanel jp = new JPanel();
  10. private JLabel jl = new JLabel("Dyanamic 2");
  11. private JButton jb = new JButton("Dynamic Panel 1");
  12. public DynamicPanel2(){
  13. add(jl);
  14. add(jb);
  15. jb.addActionListener(new ActionListener(){
  16. public void actionPerformed(ActionEvent e){
  17. //what to put?
  18. }
  19. });
  20. }
  21. public JPanel getD2(){
  22. return this;
  23. }
  24. }
Last edited by Tekmaven; Jul 1st, 2009 at 6:29 pm. Reason: Code Tags
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
cebubinary is offline Offline
18 posts
since Jan 2008
Jun 30th, 2009
0

Re: Frame and Multiple Panel ..Need Help

Hi... use code tags as explained in the stick. And what is your question/problem?
Reputation Points: 874
Solved Threads: 352
Posting Maven
BestJewSinceJC is offline Offline
2,758 posts
since Sep 2008
Jun 30th, 2009
0

Re: Frame and Multiple Panel ..Need Help

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.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
cebubinary is offline Offline
18 posts
since Jan 2008
Jun 30th, 2009
0

Re: Frame and Multiple Panel ..Need Help

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/....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.
Last edited by BestJewSinceJC; Jun 30th, 2009 at 11:33 pm.
Reputation Points: 874
Solved Threads: 352
Posting Maven
BestJewSinceJC is offline Offline
2,758 posts
since Sep 2008
Jul 1st, 2009
0

Re: Frame and Multiple Panel ..Need Help

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/....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?
Reputation Points: 10
Solved Threads: 0
Newbie Poster
cebubinary is offline Offline
18 posts
since Jan 2008
Jul 1st, 2009
0

Re: Frame and Multiple Panel ..Need Help

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.
Reputation Points: 874
Solved Threads: 352
Posting Maven
BestJewSinceJC is offline Offline
2,758 posts
since Sep 2008
Jul 1st, 2009
0

Re: Frame and Multiple Panel ..Need Help

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);
??
Reputation Points: 10
Solved Threads: 0
Newbie Poster
cebubinary is offline Offline
18 posts
since Jan 2008
Jul 1st, 2009
0

Re: Frame and Multiple Panel ..Need Help

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.
Reputation Points: 874
Solved Threads: 352
Posting Maven
BestJewSinceJC is offline Offline
2,758 posts
since Sep 2008
Jul 1st, 2009
0

Re: Frame and Multiple Panel ..Need Help

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....
Reputation Points: 10
Solved Threads: 0
Newbie Poster
cebubinary is offline Offline
18 posts
since Jan 2008
Jul 1st, 2009
0

Re: Frame and Multiple Panel ..Need Help

1.

Java Syntax (Toggle Plain Text)
  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:
Java Syntax (Toggle Plain Text)
  1. public class DynamicFrame extends JFrame{
  2. public DynamicFrame(){
  3. public void actionPerformed(ActionEvent e){
  4. add(new DynamicPanel1(this));
  5. }
  6. }

....

Java Syntax (Toggle Plain Text)
  1. public class DynamicPanel1 extends JPanel{
  2. DynamicFrame myFrame = null;
  3. public DynamicPanel1(DynamicFrame frame){
  4. myFrame = frame;
  5. public void actionPerformed(ActionEvent e){
  6. //Is this where you want to create your DynamicPanel2?
  7. //I noticed you never created one... I'll show you how you'd
  8. //create it though:
  9. new DynamicPanel2(myFrame, this);
  10. }
  11. }

....

Java Syntax (Toggle Plain Text)
  1. public class DynamicPanel2 extends JPanel{
  2. DynamicFrame myFrame;
  3. DynamicPanel1 myPanel1;
  4. public DynamicPanel2(DynamicFrame frame, DynamicPanel1 panel1){
  5. myFrame = frame;
  6. myPanel1 = panel1;
  7. }
  8. public void actionPerformed(ActionEvent e){
  9. myFrame.add(myPanel1);
  10. }


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.
Reputation Points: 874
Solved Threads: 352
Posting Maven
BestJewSinceJC is offline Offline
2,758 posts
since Sep 2008

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Java Forum Timeline: can't install JFreechart
Next Thread in Java Forum Timeline: exception in thread main java .lang.NoClassDefFoundError:Abc





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC