Results 1 to 5 of 5
  1. #1
    soulmed is offline Member
    Join Date
    Apr 2011
    Posts
    11
    Rep Power
    0

    Exclamation 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

  2. #2
    sehudson's Avatar
    sehudson is offline Senior Member
    Join Date
    Mar 2010
    Posts
    340
    Rep Power
    5

    Default

    might get better help in the AWT/Swing forum, im not sure.

  3. #3
    doWhile is offline Moderator
    Join Date
    Jul 2010
    Location
    California
    Posts
    1,642
    Rep Power
    6

    Default

    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) )

  4. #4
    RichersooN is offline Senior Member
    Join Date
    Jan 2011
    Location
    Rizal Province, Philippiines
    Posts
    167
    Rep Power
    0

    Default

    #1 You can use JFrame.validate() in every JButton's event;
    Java Code:
    Container c = getContentPane();
    c = getContentPane();
    c.validate();
    #2 You can hide the panel1 then unhide the panel2
    Last edited by RichersooN; 04-20-2011 at 08:35 PM.

  5. #5
    soulmed is offline Member
    Join Date
    Apr 2011
    Posts
    11
    Rep Power
    0

    Default

    Quote Originally Posted by RichersooN View Post
    #1 You can use JFrame.validate() in every JButton's event;
    Java Code:
    Container c = getContentPane();
    c = getContentPane();
    c.validate();
    #2 You can hide the panel1 then unhide the panel2
    Thank you it works out fine..
    Newb today, Pro tomorrow,
    Soulmed

Similar Threads

  1. Replies: 5
    Last Post: 06-14-2010, 06:10 PM
  2. Update the JFrame after change the Content Pane
    By alisonchan30 in forum AWT / Swing
    Replies: 1
    Last Post: 04-26-2010, 06:22 AM
  3. Replies: 6
    Last Post: 04-12-2010, 11:33 AM
  4. Passing data from one JFrame to another JFrame
    By tarami in forum New To Java
    Replies: 3
    Last Post: 08-06-2009, 05:44 PM
  5. Replies: 4
    Last Post: 04-22-2009, 11:02 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •