Results 1 to 4 of 4
  1. #1
    weikang is offline Member
    Join Date
    Nov 2010
    Posts
    10
    Rep Power
    0

    Default Help with JPanel and JFrame

    Hi, I need help with JPanel and JFrame.

    For example, if i had created 3 panel (JPanelA, JPanelB and JPanelC), 1 JFrame ,1 JButton(JButton1) in JPanelA and 1 JButton(JButton2).

    Firstly, when I run the application, the screen will display JPanelA in JFrame. When I click on JButton1 in JPanelA, the JPanelA will be removed and JPanelB will be added into the JFrame. And when I click the JButton2 in JPanel2, the JPanelB will be removed and JPanelC will be added into the JFrame. Also vice-versa.

    Please show me an example on how to code this and I need it urgently for my project. Thank you.

    If I had double post the same question again, please send me the link to that thread thanks and I will delete this.

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Hello weikang, what I think you want to use is a CardLayout, and you will find a great tutorial on this with sample code here: The CardLayout

    I would recommend that you have the JFrame's contentPane use the CardLayout for it's layout, and then you could have your JButtons simply call the CardLayout object's next method if you want the GUI to display the next panel in the series. I think that next(Container) will automatically go back to the first JPanel if it is called when the last component is visible. Also be sure to pass the contentPane into this method as its argument.

    Luck!

  3. #3
    weikang is offline Member
    Join Date
    Nov 2010
    Posts
    10
    Rep Power
    0

    Smile

    Thank you.

    But if I did not want to use CardLayout method, what other method can I use.

    It is because I saw some codes from other website,e.g.
    Java Code:
    private JFrame myFrame = null;
    
    private JButton getJButton1() {
      if(jButton1 == null){
          jButton1.addActionListener(new java.awt.event.ActionListener() {
          public void actionPerformed(java.awt.event.ActionEvent e) {
          JPanel panel = new JPanelB();
          myFrame.getContentPane().removeAll();
          myFrame.getContentPane().add(panel);
          myFrame.getContentPane().validate();
          myFrame.getContentPane().repaint();
          }
        });
       }
      return jButton1;
    }
    Hope to get a reply soon thank you. :):):)
    Last edited by weikang; 11-26-2010 at 01:04 AM.

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by weikang View Post
    Thank you.

    But if I did not want to use CardLayout method, what other method can I use.

    It is because I saw some codes from other website,e.g.
    Hope to get a reply soon thank you. :):):)
    Fine, use the code from the other website if you want. It still doesn't change my recommendation -- to use CardLayout. But it's up to you since it's your project.

Similar Threads

  1. JPanel not showing up in JFrame
    By rlindsey in forum AWT / Swing
    Replies: 2
    Last Post: 06-25-2010, 07:21 AM
  2. JPanel/JFrame
    By Mayur in forum New To Java
    Replies: 2
    Last Post: 12-20-2009, 05:07 AM
  3. How to add JFrame inside JPanel
    By niteshwar.bhardwaj in forum Java 2D
    Replies: 8
    Last Post: 12-13-2009, 08:41 PM
  4. problems with JPanel and JFrame
    By v1nsai in forum New To Java
    Replies: 13
    Last Post: 04-08-2009, 07:49 PM
  5. scroll a Jpanel in a JFrame
    By nidhirastogi in forum SWT / JFace
    Replies: 1
    Last Post: 09-07-2008, 03:42 AM

Tags for this Thread

Posting Permissions

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