-1

I have the following situatation:

I have a Java Swing application.

In the class that implement my GUI I have a button named Log Out tath is binding to an event listener that handle the click event, something like it:

JButton logOutButton = new JButton("LogOut");
header.add(logOutButton);

Then in the same class that implement my GUI I have declared the ActionListener that handle this event using an inner class:

    logOutButton.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent event) {
            System.out.println("logOutButton clicked !!!");
            System.exit(0);
        }
    });

In this moment when I click the logOutButton button the program end. I would that instead exit it is restarted by running a specific class called LoginForm (the class that implement the login form GUI)

What can I do to do this thing?

Tnx

Andrea

AndreaNobili
  • 40,955
  • 107
  • 324
  • 596

1 Answers1

1

You don't really need to close/open window junky approach at all. Just use Card Layout:

  1. set Frame's content pane's layout to card layout.

    getContentPane().setLayout(new CardLayout());
    
  2. Put your different Form's content code inside different panel and add them to the content pane with their corresponding name, for example:

     getContetnPane().add(logInFormPanel, "logIn Form");
    
  3. Now you can simulate the card to appear whenever necessary by calling CardLayout.show(Container parent, String name). For example:

     logOutButton.addActionListener(new ActionListener() {
             public void actionPerformed(ActionEvent event) {
                  System.out.println("logOutButton clicked !!!");
              CardLayout cl = (CardLayout)(getContentPane().getLayout());
              cl.show(getContentPane(), "logIn Form");
    
        }
    });
    

Check out a CardLayout demo from my another answer.

Community
  • 1
  • 1
Sage
  • 15,290
  • 3
  • 33
  • 38