I've looked around online at how to open a new JFrame from an existing JFrame. I've found that apparently the best way to do this is dispose of the existing JFrame and open the new JFrame - however this is a problem.
I have a login form, one the users logs in, the login frame is disposed and the main frame is set visible.
import javax.swing.JFrame;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
public class client {
public static void main(String[] args) {
initialize();
}
private static void initialize() {
JFrame loginFrame = new JFrame("Login");
loginFrame.setBounds(100, 100, 300, 300);
loginFrame.setResizable(false);
loginFrame.setLocationRelativeTo(null);
loginFrame.setDefaultCloseOperation(loginFrame.HIDE_ON_CLOSE);
loginFrame.getContentPane().setLayout(null);
JFrame mainFrame = new JFrame("Main");
mainFrame.setBounds(100, 100, 300, 197);
mainFrame.setResizable(false);
mainFrame.setLocationRelativeTo(null);
mainFrame.setDefaultCloseOperation(mainFrame.EXIT_ON_CLOSE);
mainFrame.getContentPane().setLayout(null);
JButton loginButton = new JButton("Login");
loginButton.setBounds(102, 133, 89, 23);
loginButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
loginButton.setEnabled(false);
loginFrame.dispose();
mainFrame.setVisible(true);
}
});
loginFrame.getContentPane().add(loginButton);
loginFrame.setVisible(true);
}
}
However if the user launches the client and then decides not to login and closes it, the process remains running in the background?
I feel like this is a really stupid question and I am sorry if so, but I've looked around and couldn't find any workarounds for this. Am I ok to not dispose of the login frame and just hide it and set them both to EXIT_ON_CLOSE?
Thanks in advance!