0

There is a class which has a static method : ChangePanel which will change the content of a static JPanel to new content. Frame is the variable name of the static JPanel .

 public static void ChangePanel(JPanel jp)
    {

        Javaassignment1b.Driver.Frame.getContentPane().removeAll();

        Javaassignment1b.Driver.Frame.add(jp);

        Javaassignment1b.Driver.Frame.validate();

    } 

This static method works as expected when it is implemented on a JPanel whose functionality is added by actionlistener.

Example

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

        loginbutton.addActionListener(new LoginButtonListener());

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

     }

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

            char[] password = pwfield.getPassword();

            //convert from char array to String
            String password2 = new String(password);

            admin = new Admin();

            if (admin.checkPassword(password2))
            {
                //show Admin panel
                //works in this case
                AdminMenuPanel amp = new AdminMenuPanel();
                Utility.ChangePanel(amp);


            }
            else
            {
                //text box appear to state password is incorrect
                ErrorMessagePanel emp = new ErrorMessagePanel("Wrong password, please try again");


            }


        }





    }



    JLabel pwlabel;
    JPasswordField pwfield;
    JButton loginbutton;
    Admin admin;

}

I dont understand why is it not working when I try to change the content of the static JPanel through the return value of JOptionPane

public class RetryPanel extends JPanel
{
    RetryPanel()
    {
        jp = new JOptionPane();

        //reply will be 1 for No , 0 for yes
        int reply = JOptionPane.showConfirmDialog(null,"Would you like to start a new game ???","Message Title",JOptionPane.YES_NO_OPTION );

        if ( reply == 0)
        {
            //player wants to play a new game , retry


        }
        else
        {
            //player wants to quit
            //doesnt work in this case
            SelectAdminUserPanel saup = new SelectAdminUserPanel();
            Utility.ChangePanel(saup);
        }

    }

    JOptionPane jp;
}

Using a debugger shows that my code is executed however it just doesnt change the static Jpanel content , I suspect it may have something to do with threads , can someone help me resolve this issue

note : I know CardLayout would be a better alternative for changing JPanel but for legacy reasons , I need to figure this out

Thanks

Computernerd
  • 7,378
  • 18
  • 66
  • 95
  • 1
    1) I fear that you've got a bug in code not shown. 2) I also worry when I see static being used where it shouldn't be used. Consider creating and posting your [minimal code example program](http://stackoverflow.com/help/mcve) for us to review, test, and possibly fix. – Hovercraft Full Of Eels Apr 23 '14 at 03:19
  • `static` is not your friend... – MadProgrammer Apr 23 '14 at 03:24
  • 1) For many components in one space, use a [`CardLayout`](http://docs.oracle.com/javase/7/docs/api/java/awt/CardLayout.html) as seen in this [short example](http://stackoverflow.com/a/5786005/418556). 2) A single blank line of white space in source code is *always* enough. Blank lines after `{` or before `}` are also typically redundant. – Andrew Thompson Apr 23 '14 at 03:36

0 Answers0