Results 1 to 5 of 5
Thread: How do I update a JFrame?
- 04-20-2011, 08:21 PM #1
Member
- Join Date
- Apr 2011
- Posts
- 11
- Rep Power
- 0
How do I update a JFrame?
I'm trying to switch from one JPanel to another in my JFrame and I just can't get it to Update the JFrame. I've reproduce a lighter version of my code to make it easyer to read. The original one got more stuff and the JPanel with Events are in seperate files.
Here's the simple version of the code:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class test {
public static JFrame frame = new JFrame ("MyPanel");
public static JPanel MyPanel1() {
JPanel jp = new JPanel();
JButton page1BTN = new JButton ("Page 1");
page1BTN.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
page1BTN_Clicked(e);
}
});
jp.setPreferredSize (new Dimension (234, 175));
jp.setLayout (null);
jp.add (page1BTN);
page1BTN.setBounds (55, 70, 100, 20);
return jp;
}
public static JPanel MyPanel2() {
JPanel jp = new JPanel();
JButton page2BTN = new JButton ("Page 2");
page2BTN.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
page2BTN_Clicked(e);
}
});
jp.setPreferredSize (new Dimension (234, 175));
jp.setLayout (null);
jp.add (page2BTN);
page2BTN.setBounds (55, 70, 100, 20);
return jp;
}
protected static void page2BTN_Clicked(ActionEvent e) {
frame.getContentPane().removeAll();
frame.getContentPane().add (MyPanel1());
frame.validate();
}
protected static void page1BTN_Clicked(ActionEvent e) {
frame.getContentPane().removeAll();
frame.getContentPane().add (MyPanel1());
frame.validate();
}
public static void main (String[] args) {
frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add (MyPanel1());
frame.pack();
frame.setVisible (true);
}
}Newb today, Pro tomorrow,
Soulmed
- 04-20-2011, 08:25 PM #2
might get better help in the AWT/Swing forum, im not sure.
- 04-20-2011, 08:28 PM #3
Moderator
- Join Date
- Jul 2010
- Location
- California
- Posts
- 1,605
- Rep Power
- 5
First, please use the code tags. Second, you might be better off using a CardLayout (see How to Use CardLayout (The Java™ Tutorials > Creating a GUI With JFC/Swing > Laying Out Components Within a Container) )
- 04-20-2011, 08:32 PM #4
Senior Member
- Join Date
- Jan 2011
- Location
- Rizal Province, Philippiines
- Posts
- 167
- Rep Power
- 0
#1 You can use JFrame.validate() in every JButton's event;
#2 You can hide the panel1 then unhide the panel2Java Code:Container c = getContentPane(); c = getContentPane(); c.validate();
Last edited by RichersooN; 04-20-2011 at 08:35 PM.
- 04-20-2011, 08:45 PM #5
Member
- Join Date
- Apr 2011
- Posts
- 11
- Rep Power
- 0
Similar Threads
-
to pass a parameter from a jframe children to its jframe mother
By anix in forum NetBeansReplies: 5Last Post: 06-14-2010, 06:10 PM -
Update the JFrame after change the Content Pane
By alisonchan30 in forum AWT / SwingReplies: 1Last Post: 04-26-2010, 06:22 AM -
Passing data from one JFrame to another JFrame. - need help.
By Unsub in forum New To JavaReplies: 6Last Post: 04-12-2010, 11:33 AM -
Passing data from one JFrame to another JFrame
By tarami in forum New To JavaReplies: 3Last Post: 08-06-2009, 05:44 PM -
How to make a Jframe un-focusable when another Jframe is active?
By Robert_85 in forum Advanced JavaReplies: 4Last Post: 04-22-2009, 11:02 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks