0

I am trying to transit from a UserAdminPanel to AdminLogin within the same JPanel when I press the Admin button.

UserAdmin Panel

enter image description here

transit to AdminLogin Panel

enter image description here

The problem I have now is that I am opening up a new panel instead of changing the current panel to the new panel.

This is my code for the UserAdminPanel

public class SelectAdminUserPanel extends JPanel 
{
    public SelectAdminUserPanel()
    {
        setLayout(new GridLayout(3,1));
        JButton b1 = new JButton("User Login");
        JButton b2 = new JButton("Admin Login");
        JButton b3 = new JButton("Exit");

        b1.addActionListener(new  SelectUserButtonListener() );
        b2.addActionListener(new SelectAdminButtonListener());
        b3.addActionListener(new SelectExitButtonListener() );

        add(b1);
        add(b2);
        add(b3);
    }

    private class SelectAdminButtonListener implements ActionListener
    {

        public void actionPerformed(ActionEvent event)
        {
            AdminModule am = new AdminModule();

              am.run();   
        }
    }

    private class SelectUserButtonListener implements ActionListener
    {
        public void actionPerformed(ActionEvent event)
        {
              GameModule gm = new GameModule();
              gm.run(); 
        }
    }

    private class SelectExitButtonListener implements ActionListener
    {
        public void actionPerformed (ActionEvent event)
        {

        }
    }
}

This is the code for the AdminLogin Panel

public class AdminLoginPanel extends JPanel
{
    AdminLoginPanel()
    {         
        JLabel pwlabel = new JLabel("Password");
        JPasswordField pwfield = new JPasswordField(20);
        JButton loginbutton = new JButton("Login");

        add(pwlabel);
        add(pwfield);
        add(loginbutton);
     }
}

I have looked at the following example and this example but it's not very applicable because it talks about CardLayout instead of like rewriting the current JPanel.

Community
  • 1
  • 1
Computernerd
  • 7,378
  • 18
  • 66
  • 95
  • can't you just use remove and add methods of the parent container where your panels will be lying? – Hirak Apr 17 '14 at 09:36
  • What exactly is the problem of replacing one panel with another in the same window instead of redoing the whole layout? – Ordous Apr 17 '14 at 09:41
  • @Ordous that would be wonderful , how do i go about doing it – Computernerd Apr 17 '14 at 09:49
  • The log in panel should be displayed in a `JOptionPane`. Apart from that, I'd use `CardLayout`. Why do you think it is not applicable here? – Andrew Thompson Apr 17 '14 at 09:49
  • See [How do I create screenshots?](http://meta.stackexchange.com/questions/99734/how-do-i-create-a-screenshot-to-illustrate-a-post) (for tips on making *great* screenshots). Specific tip: **alt +** Print Screen on Windows will crop the screen shot to the active window. ;) – Andrew Thompson Apr 17 '14 at 09:54
  • @Computernerd exactly by using CardLayout. Have one panel with this layout that has several child panels (One with the buttons, other with the login). Then you can switch between child panels on button press while stating in the same window – Ordous Apr 17 '14 at 09:57

1 Answers1

1

I think that you should have a reference to your main frame and just remove the components from it based on the button pressed and add only the required components. From what you say, UserAdminPanel is your main panel. I think it's added to a frame for which you can obtain a reference. When you click a button, you want to remove all the content shown on it and display only what the button clicked should show. I think it should look something like this:

private class SelectAdminButtonListener implements ActionListener {

        public void actionPerformed(ActionEvent event) {
             frame.getContentPane().removeAll();
             AdminModule am = new AdminModule();
             frame.add(am.getNewPanel());
             frame.pack();                    
             // am.run();   //it's not clear what does for you
        }

}

Where the method getNewPanel() would return the underlying JPanel. I'm assuming that AdminModule has a reference to the AdminLoginPanel.

Slimu
  • 2,301
  • 1
  • 22
  • 28