0

So I'm creating an application. I want main to initialize a login window, which I've done. And for now I want the login window to close whenever I click on the button, and when it closes, a new window (called MainWindow) opens with different buttons/info/text.

All these classes are separate, but in the same package.

The problem is: The main window opens when I click login, however the login window doesn't terminate/close.

My main method, from which I call the login window:

import javax.swing.*;

public class Main {

    // Display Login Window
    public static void main(String[] args) {
        SwingUtilities.invokeLater(() -> {
            new LoginWindow();
            LoginWindow.createAndShowLoginGUI();
        });
    }
}

My login window class:

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class LoginWindow extends JFrame implements ActionListener {

    public void prepareLoginGUI() {

        // Frame with GridBagLayout
        JFrame loginFrame = new JFrame("Login Window");
        loginFrame.setSize(1200, 800);
        loginFrame.setLayout(new GridBagLayout());


        // Button for logging in, with respective GridBagConstraint
        // setFocusable is set to false to take out the border around the text
        JButton loginButton = new JButton("Login");
        loginButton.addActionListener(this::actionPerformed);
        loginButton.setActionCommand("Open");
        GridBagConstraints lButtonC = new GridBagConstraints();
        loginButton.setFocusable(false);


        // Username text-field and JLabel with respective GridBagConstraints
        JTextField tfUsername = new JTextField(15);
        GridBagConstraints tfUserC = new GridBagConstraints();
        JLabel txtUser = new JLabel("Username: ");
        GridBagConstraints txtUserC = new GridBagConstraints();


        // Password text-field and JLabel with respective GridBagConstraints
        JPasswordField tfPassword = new JPasswordField(15);
        GridBagConstraints tfPassC = new GridBagConstraints();
        JLabel txtPassword = new JLabel("Password: ");
        GridBagConstraints txtPassC = new GridBagConstraints();


        // Add all components to the JFrame
        // Making sure to add the text before the text-fields
        loginFrame.add(txtUser, txtUserC);
        loginFrame.add(tfUsername, tfUserC);
        loginFrame.add(txtPassword, txtPassC);
        loginFrame.add(tfPassword, tfPassC);
        loginFrame.add(loginButton, lButtonC);

        // Show and set close parameters
        loginFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        loginFrame.setVisible(true);
    }

    // Instructions for when the login button is clicked
    // Close this window, if button == open, which it does
    @Override
    public void actionPerformed(ActionEvent e) {
        String cmd = e.getActionCommand();
        if (cmd.equals("Open")) {
            this.dispose();
            this.setVisible(false);
            new MainWindow();
            MainWindow.createAndShowMainWGUI();
        }
    }

    // Callable from Main class
    public static void createAndShowLoginGUI() {
        LoginWindow loginW = new LoginWindow();
        loginW.prepareLoginGUI();

    }

}

My Main Window Class:

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class MainWindow extends JFrame implements ActionListener {

    public void prepareMainWGUI(){
        // Frame with GridBagLayout
        JFrame loginFrame = new JFrame("Main Window");
        loginFrame.setSize(1200, 800);
        loginFrame.setLayout(new GridBagLayout());


        // Username text-field and JLabel with respective GridBagConstraints
        JLabel txtUser = new JLabel("It worked!");
        GridBagConstraints txtUserC = new GridBagConstraints();

        loginFrame.add(txtUser, txtUserC);

        loginFrame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        loginFrame.setVisible(true);
    }

    // Callable from Main class
    public static void createAndShowMainWGUI() {
        MainWindow mainWind = new MainWindow();
        mainWind.prepareMainWGUI();
    }

}

If you use this code, when the main window opens, the login window will be right behind it.

data_pi
  • 801
  • 2
  • 11
  • 30
  • See [The Use of Multiple JFrames, Good/Bad Practice?](http://stackoverflow.com/q/9554636/418556) – Andrew Thompson Jun 04 '17 at 05:21
  • @AndrewThompson Thank you for the link. I'm wondering how would I change the display of the frame then? Should I use panels for each display i want? – data_pi Jun 04 '17 at 05:42

1 Answers1

1

You can use when pressing button

System.exit(0);

This will terminate your application. To close one window use(if you have open multiple windows) without affecting to others,

setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE)

Then, to open new one,

new frameName().setvisible(true);

Example:

JButton closeButton = new JButton("Close");

closeButton .addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent e)
    {
        this.dispose();//current(close only this) frame
        new frameName().setvisible(true);
    }
});

Take a look at this question.

UPDATE:

In below line you have extends JFrame but you didnot use it.

public class LoginWindow extends JFrame implements ActionListener {

Rather than that you have create new object from JFrame

JFrame loginFrame = new JFrame("Login Window");

All the component you are adding to the loginFrame. But when actionPerformed trying to this.dispose(), since you are not using extended JFrame nothing will happen.

Solution:

Declare your JFrame as a instance variable:

public class LoginWindow implements ActionListener {
    JFrame loginFrame;
    public void prepareLoginGUI() {

Then change,

this.dispose();

To:

loginFrame.dispose();

In this this.dispose(); call to the extended JFrame, no effect for your JFrame object(loginForm).

And one last thing, make sure to remove extends JFrame. Because you are not doing anything with extended JFrame. Read this post about extended JFrame.

Blasanka
  • 21,001
  • 12
  • 102
  • 104