| | |
Frame and Multiple Panel ..Need Help
![]() |
•
•
Join Date: Jan 2008
Posts: 18
Reputation:
Solved Threads: 0
hi im new to java and im learning about how to add/remove panels from another panel...
This is my main frame
this is my panel 1
this is my panel2
This is my main frame
java Syntax (Toggle Plain Text)
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
java Syntax (Toggle Plain Text)
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; } }
java Syntax (Toggle Plain Text)
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; } }
Last edited by Tekmaven; Jul 1st, 2009 at 6:29 pm. Reason: Code Tags
•
•
Join Date: Sep 2008
Posts: 1,557
Reputation:
Solved Threads: 195
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.
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.
Out.
•
•
Join Date: Jan 2008
Posts: 18
Reputation:
Solved Threads: 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/....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.
•
•
Join Date: Sep 2008
Posts: 1,557
Reputation:
Solved Threads: 195
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.
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.
Out.
•
•
Join Date: Jan 2008
Posts: 18
Reputation:
Solved Threads: 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);
??
•
•
Join Date: Sep 2008
Posts: 1,557
Reputation:
Solved Threads: 195
1.
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:
....
....
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.
Java Syntax (Toggle Plain Text)
add( new DynamicPanel1().getD1());
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)
public class DynamicFrame extends JFrame{ public DynamicFrame(){ public void actionPerformed(ActionEvent e){ add(new DynamicPanel1(this)); } }
....
Java Syntax (Toggle Plain Text)
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); } }
....
Java Syntax (Toggle Plain Text)
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.
Out.
![]() |
Similar Threads
- How to display the details in right frame? (JavaScript / DHTML / AJAX)
- Design Problem: Dealing with multiple time frames (C++)
- Making frame background 'Transparent' (Visual Basic 4 / 5 / 6)
- doing a program blinded (Java)
- Firefox keeps opening multiple tabs by itself everytime I open Firefox (Viruses, Spyware and other Nasties)
- Cannot Access Control Panel or Add/Remove Programs!!! (Viruses, Spyware and other Nasties)
- wxBoxSizer with 2 panels - panel size (Python)
- multiple infections, please help... (Viruses, Spyware and other Nasties)
- Best Video Cards (Monitors, Displays and Video Cards)
- Dell Latitude - Multiple Monitors not working (IT Professionals' Lounge)
Other Threads in the Java Forum
- Previous Thread: can't install JFreechart
- Next Thread: exception in thread main java .lang.NoClassDefFoundError:Abc
| Thread Tools | Search this Thread |
-xlint actionlistener android api applet application array arrays automation bi binary blackberry block bluetooth character class client code compile compiler component consumer database desktop developmenthelp eclipse error fractal freeze ftp functiontesting game gameprogramming givemetehcodez graphics gui health html ide image integer j2me j2seprojects java javac javaee javaprojects jetbrains jni jpanel jtable julia learningresources lego linked linux list mac main map method methods mobile myregfun netbeans notdisplaying number online printf problem program project qt recursion researchinmotion rotatetext rsa scanner screen server set singleton sms sort spamblocker sql string swing system textfields threads time title tree tutorial-sample update variablebinding windows working xor






