Reputation: 11
and no... system.exit(0) is NOT what I am looking for.
I have a MainPage GUI with 5 buttons, clicking 4 of the buttons brings up separate GUI's for different things, one open a chat server, one opens a file dialog, ETC. however, if I use system.exit(0); to close this new GUI it also closes the MainPage GUI and that is not what I want. I have looked into this.dispose(); however I am not sure how to use it.
exit = new JButton("Exit");
exit.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
Runtime.getRuntime().exit(0); // this is the same thing as system.exit(0);
}
});
exit.setBounds(BUTTON_INDENT, 4 * BUTTON_HEIGHT + 5 * BUTTON_INDENT,
BUTTON_WIDTH, BUTTON_HEIGHT);
The point is for this to only close the Maximized GUI and NOT the minimized ones as well.
EDIT:
class MyClient implements ActionListener {
Socket s;
DataInputStream dis;
DataOutputStream dos;
JButton sendButton, logoutButton, loginButton, exitButton;
JFrame chatWindow;
JTextArea txtBroadcast;
JTextArea txtMessage;
JList usersList;
// ////////////////////////
public void displayGUI() {
chatWindow = new JFrame();
txtBroadcast = new JTextArea(5, 30);
txtBroadcast.setEditable(false);
txtMessage = new JTextArea(2, 20);
usersList = new JList();
sendButton = new JButton("Send");
logoutButton = new JButton("Log out");
loginButton = new JButton("Log in");
exitButton = new JButton("Exit");
JPanel center1 = new JPanel();
center1.setLayout(new BorderLayout());
center1.add(new JLabel("Broad Cast messages from all online users",
JLabel.CENTER), "North");
center1.add(new JScrollPane(txtBroadcast), "Center");
JPanel south1 = new JPanel();
south1.setLayout(new FlowLayout());
south1.add(new JScrollPane(txtMessage));
south1.add(sendButton);
JPanel south2 = new JPanel();
south2.setLayout(new FlowLayout());
south2.add(loginButton);
south2.add(logoutButton);
south2.add(exitButton);
JPanel south = new JPanel();
south.setLayout(new GridLayout(2, 1));
south.add(south1);
south.add(south2);
JPanel east = new JPanel();
east.setLayout(new BorderLayout());
east.add(new JLabel("Online Users", JLabel.CENTER), "East");
east.add(new JScrollPane(usersList), "South");
chatWindow.add(east, "East");
chatWindow.add(center1, "Center");
chatWindow.add(south, "South");
chatWindow.pack();
chatWindow.setTitle("Login for Chat");
chatWindow.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
chatWindow.setVisible(true);
sendButton.addActionListener(this);
logoutButton.addActionListener(this);
loginButton.addActionListener(this);
exitButton.addActionListener(this);
logoutButton.setEnabled(false);
loginButton.setEnabled(true);
txtMessage.addFocusListener(new FocusAdapter() {
public void focusGained(FocusEvent fe) {
txtMessage.selectAll();
}
});
chatWindow.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent ev) {
if (s != null) {
JOptionPane.showMessageDialog(chatWindow,
"u r logged out right now. ", "Exit",
JOptionPane.INFORMATION_MESSAGE);
logoutSession();
}
System.exit(0);
}
});
}
// /////////////////////////
public void actionPerformed(ActionEvent ev) {
JButton temp = (JButton) ev.getSource();
if (temp == sendButton) {
if (s == null) {
JOptionPane.showMessageDialog(chatWindow,
"u r not logged in. plz login first");
return;
}
try {
dos.writeUTF(txtMessage.getText());
txtMessage.setText("");
} catch (Exception excp) {
txtBroadcast.append("\nsend button click :" + excp);
}
}
if (temp == loginButton) {
String uname = JOptionPane.showInputDialog(chatWindow,
"Enter Your lovely nick name: ");
if (uname != null)
clientChat(uname);
}
if (temp == logoutButton) {
if (s != null)
logoutSession();
}
if (temp == exitButton) {
if (s != null) {
JOptionPane.showMessageDialog(chatWindow,
"u r logged out right now. ", "Exit",
JOptionPane.INFORMATION_MESSAGE);
logoutSession();
}
System.exit(0);
}
}
This is the inner class for the code, as you can see here
if (temp == exitButton) {
if (s != null) {
JOptionPane.showMessageDialog(chatWindow,
"u r logged out right now. ", "Exit",
JOptionPane.INFORMATION_MESSAGE);
logoutSession();
}
System.exit(0);
}
the code will end the JVM anyway no matter what the default close operation is. I am not using default close operation because:
f.setUndecorated(true);
I am not using the default "windows window theme"
Upvotes: 1
Views: 495
Reputation: 386
As you know you can create a JButton exit = new JButton with an exit Icon(yes to be nice...) and you can do:
1) window.dispose()
(Released any source linked with the window)
2)or window.setVisible(false)
just make the window(unvisible)
3)When you make a window(JFrame) you have the option to choose how it will respond(JFrame.EXIT_ON_CLOSE,JFrame.DISPOSE_ON_CLOSE,JFrame.DO_NOTHING_ON_CLOSE)
Gennerally the java programm will automatically terminate
if no windows are opened or just all the operation has finished(no loops running,etc)
If you didn't used the window.setUndecorated(false) you could use Window Listener as follows:
window.addWindowListener(new WindowAdapter(){
@Override
public void windowClosing(WindowEvent e) {
//This method is called when the X buttons is Clicked/Released
}});
Upvotes: 0
Reputation: 3019
Use the setDefaultCloseOperation(int)
command on the JFrame
.
frame.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
Will dispose of the frame, but the JVM will stay alive - unless all windows have been closed, as pointed out in the comment.
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
Will dispose of the frame and stop the JVM.
By default this is set to HIDE_ON_CLOSE
.
Upvotes: 6