Jframe.Dispose() VS System.Exit()

JFrame.dispose() vs System.exit()

System.exit(); causes the Java VM to terminate completely.

JFrame.dispose(); causes the JFrame window to be destroyed and cleaned up by the operating system. According to the documentation, this can cause the Java VM to terminate if there are no other Windows available, but this should really just be seen as a side effect rather than the norm.

The one you choose really depends on your situation. If you want to terminate everything in the current Java VM, you should use System.exit() and everything will be cleaned up. If you only want to destroy the current window, with the side effect that it will close the Java VM if this is the only window, then use JFrame.dispose().

Difference between dispose and exit on close in java

EXIT_ON_CLOSE will terminate the program.

DISPOSE_ON_CLOSE will call dispose() on the frame, which will make it disappear and remove the resources it is using. You cannot bring it back, unlike hiding it.

See aslo JFrame.dispose() vs System.exit()

dispose() vs setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

To answer 1.

dispose() on a JFrame will destroy the window and have the operating system clean up after it, if its the only JFrame left, the Java VM might terminate. So, in summary, it will close the window, and if the JFrame is the last remaining window of your application, it might (or might not) terminate your VM.

setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE) however, will call System.exit() when the JFrame is closed, terminating the VM regardless of there being any other active JFrames in your app.

So, they are two different things :-)

To answer your second question, you could test it. But honestly, I wouldn't be to worried about this.

Basic Java Swing, how to exit and dispose of your application/JFrame

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

public class ClosingFrame extends JFrame {

private JMenuBar MenuBar = new JMenuBar();
private JFrame frame = new JFrame();
private static final long serialVersionUID = 1L;
private JMenu File = new JMenu("File");
private JMenuItem Exit = new JMenuItem("Exit");

public ClosingFrame() {
File.add(Exit);
MenuBar.add(File);
Exit.addActionListener(new ExitListener());
WindowListener exitListener = new WindowAdapter() {

@Override
public void windowClosing(WindowEvent e) {
int confirm = JOptionPane.showOptionDialog(frame,
"Are You Sure to Close this Application?",
"Exit Confirmation", JOptionPane.YES_NO_OPTION,
JOptionPane.QUESTION_MESSAGE, null, null, null);
if (confirm == JOptionPane.YES_OPTION) {
System.exit(0);
}
}
};
frame.addWindowListener(exitListener);
frame.setDefaultCloseOperation(EXIT_ON_CLOSE);
frame.setJMenuBar(MenuBar);
frame.setPreferredSize(new Dimension(400, 300));
frame.setLocation(100, 100);
frame.pack();
frame.setVisible(true);
}

private class ExitListener implements ActionListener {

@Override
public void actionPerformed(ActionEvent e) {
int confirm = JOptionPane.showOptionDialog(frame,
"Are You Sure to Close this Application?",
"Exit Confirmation", JOptionPane.YES_NO_OPTION,
JOptionPane.QUESTION_MESSAGE, null, null, null);
if (confirm == JOptionPane.YES_OPTION) {
System.exit(0);
}
}
}

public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {

@Override
public void run() {
ClosingFrame cf = new ClosingFrame();
}
});
}
}

From which thread should System.exit() be called in a Swing-app?

You should not be calling System.exit() if you can help it.

The best way to exit a java process is to let all threads exit normally. This will terminate the VM.

In your main JFrame, you should setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE).

Then you can call frame.dispose() to close the JFrame and exit the EDT.

System.exit(0) vs JFrame.EXIT_ON_CLOSE

If you look at the JFrame code, it does:

 protected void processWindowEvent(WindowEvent e) {
super.processWindowEvent(e);

if (e.getID() == WindowEvent.WINDOW_CLOSING) {
switch(defaultCloseOperation) {
...
case EXIT_ON_CLOSE:
// This needs to match the checkExit call in
// setDefaultCloseOperation
System.exit(0);
break;
}
}
}

So, it's exactly the same thing. I would just set EXIT_ON_CLOSE if that's what you want it to do.

how to close only one awt Frame without closing other?

System.exit(); causes the Java VM to terminate completely.

So you need to use JFrame.dispose(); that causes the JFrame window to be destroyed and cleaned up by the operating system.

Frame f2 = new Frame();
f2.setVisible(true);
f2.setLayout(null);
f2.setSize(500,500);
f2.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent w)
{
f2.dispose();
}
});

Explanation from this post

Default Close Operation of a JFrame isn't working

If you want to exit your application with the button than you can use System.exit() or frame.dispose().

but be careful with System.exit() as this will terminate the JVM.

So it is better to first confirm from user before this.
with JOptionPane.showConfirmDialog();

private void bSalirActionPerformed(java.awt.event.ActionEvent evt){ 

int exit = JOptionPane.showConfirmDialog(
frame,
"Are you sure you want to exit the application?",
"Exit Application",
JOptionPane.YES_NO_OPTION);
if(JOptionPane.YES_OPTION == exit){
frame.dispose(); // or System.exit(1);
}
}


Related Topics



Leave a reply



Submit