private void windowClosing(java.awt.event.WindowEvent evt)
{
int confirmed = JOptionPane.showConfirmDialog(null, "Exit Program?","EXIT",JOptionPane.YES_NO_OPTION);
if(confirmed == JOptionPane.YES_OPTION)
{
dispose();
}
}
I want to close program by pressing Close Window Button with confirmation...But when I choose "No" to back to my Jframe, it still helps me to exit the program???
From what i understand you want something like this
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
int confirmed = JOptionPane.showConfirmDialog(null,
"Are you sure you want to exit the program?", "Exit Program Message Box",
JOptionPane.YES_NO_OPTION);
if (confirmed == JOptionPane.YES_OPTION) {
dispose();
}
}
});
If you want to use it on some button do similiar function to button. Put listener on it and do same. But I'm not sure if I get your question right. But If you want to use button use ActionListener and action performed method.
check question - Java - Message when closing JFrame Window
JFrame frame = new JFrame();
// ...
frame.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent evt) {
int resp = JOptionPane.showConfirmDialog(frame, "Are you sure you want to exit?",
"Exit?", JOptionPane.YES_NO_OPTION);
if (resp == JOptionPane.YES_OPTION) {
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
} else {
frame.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
}
}
});
Try this, it's working for me.
private void windowClosing(java.awt.event.WindowEvent evt) {
int confirmed = JOptionPane.showConfirmDialog(null, "Exit Program?","EXIT",JOptionPane.YES_NO_OPTION);
if(confirmed == JOptionPane.YES_OPTION)
{
dispose();
}
} else {
setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
Related
I looking for create mechanism for messaging when exiting and changes has been made.
I want it to display a messagebox asking the user :
( Are you sure you don't want to save the changes - yes / no)
Upon closing the form by clicking on the button named 'Exit' or when use close button, ''X". Don't know the syntax for it, can someone help me please?
Here's some basic code on how to do it:
public class ClosingFrame extends JFrame {
public ClosingFrame() {
super("Shutdown hook");
setSize(400, 400);
setLocationByPlatform(true);
setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE); /* important */
addWindowListener(new WindowAdapter() {
#Override
public void windowClosing(WindowEvent e) {
super.windowClosing(e);
int showConfirmDialog = JOptionPane.
showConfirmDialog(ClosingFrame.this, "Do you want to save?");
if (showConfirmDialog == JOptionPane.YES_OPTION) {
System.out.println("saved");
System.exit(0);
} else if (showConfirmDialog == JOptionPane.NO_OPTION) {
System.out.println("not saved");
System.exit(0);
} else {
System.out.println("aborted");
// do nothing
}
}
});
setVisible(true);
}
public static void main(String[] args) {
EventQueue.invokeLater(() -> new ClosingFrame());
}
}
I have a non-modal dialog with two input text fields shown with the JOptionPane with OK and CANCEL buttons. I show the dialog as below.
JTextField field_1 = new JTextField("Field 1");
JTextField field_2 = new JTextField("Field 2");
Object[] inputField = new Object[] { "Input 1", field_1,
"Input_2", field_2 };
JOptionPane optionPane = new JOptionPane(inputField,
JOptionPane.QUESTION_MESSAGE, JOptionPane.OK_CANCEL_OPTION);
JDialog dialog = optionPane.createDialog(null, "Input Dialog");
dialog.setModal(false);
dialog.setVisible(true);
How can i get the return value from the dialog? Means i need to get whether Ok or CANCEL button is pressed. How can achieve this?
One way would be to add a ComponentListener to the dialog and listen for its visibility to change,
dialog.addComponentListener(new ComponentListener() {
#Override
public void componentResized(ComponentEvent e) { }
#Override
public void componentMoved(ComponentEvent e) { }
#Override
public void componentShown(ComponentEvent e) { }
#Override
public void componentHidden(ComponentEvent e) {
if ((int) optionPane.getValue()
== JOptionPane.YES_OPTION) {
// do YES stuff...
} else if ((int) optionPane.getValue()
== JOptionPane.CANCEL_OPTION) {
// do CANCEL stuff...
} else {
throw new IllegalStateException(
"Unexpected Option");
}
}
});
Note: you should probably use the ComponentAdapter instead; I'm showing the whole interface for illustration.
Using getValue() will tell you how the dialog was closed. Since it's non-modal, you'll need to get that information once the dialog is closed, probably using a Thread that will wait that your dialog is closed to return the information. You don't give any details on what needs that information, so using another Thread might not be the best solution for you.
I want to know how to cause a program to exit upon selecting the X button of a showMessageDialog dialog box.
Currently whenever I do this, it simply continues running the code or, in the case of confirm or option dialog boxes, selects the 'Yes' option. Is it possible to include this kind of command in the code for the dialog box? For example:
JOptionPane.showMessageDialog(null, "Your message here");
How would I edit the output so that the X button closes the program?
Will I have to change the showMessageDialog to another type of dialog box?
I dont know if is this what you want but i put a confirm box in a program:
(...)
import org.eclipse.swt.widgets.MessageBox;
(...)
createButton(buttons, "&Exit", "Exit", new MySelectionAdapter() {
#Override
public void widgetSelected(SelectionEvent evt) {
MessageBox messageBox = new MessageBox(getShell(), SWT.YES | SWT.NO | SWT.ICON_QUESTION);
messageBox.setMessage("Are you sure?");
messageBox.setText("Exit");
if (messageBox.open() == SWT.YES) {
getParent().dispose();
}
}
});
And looking at online javadoc (java 6) or (java 1.4), you have another option:
Direct Use:
To create and use an JOptionPane directly, the standard pattern is roughly as follows:
JOptionPane pane = new JOptionPane(arguments);
pane.set.Xxxx(...); // Configure
JDialog dialog = pane.createDialog(parentComponent, title);
dialog.show();
Object selectedValue = pane.getValue();
if(selectedValue == null)
return CLOSED_OPTION;
//If there is not an array of option buttons:
if(options == null) {
if(selectedValue instanceof Integer)
return ((Integer)selectedValue).intValue();
return CLOSED_OPTION;
}
//If there is an array of option buttons:
for(int counter = 0, maxCounter = options.length;
counter < maxCounter; counter++) {
if(options[counter].equals(selectedValue))
return counter;
}
return CLOSED_OPTION;
showMessageDialog() doesn't have a return value. Here is an example with showOptionsDialog().
public class Test
{
public static void main(String[] args){
final JFrame frame = new JFrame();
JPanel panel = new JPanel();
JButton button = new JButton("Test");
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) {
int result = JOptionPane.showOptionDialog(null,
"Your message here", "", JOptionPane.DEFAULT_OPTION,
JOptionPane.PLAIN_MESSAGE, null, new String[] {"OK"}, "OK");
if (result == JOptionPane.CLOSED_OPTION) {
frame.dispose();
}
}
});
panel.add(button);
frame.setContentPane(panel);
frame.pack();
frame.setVisible(true);
}
}
I have an option pane that is displayed upon closing my Application (windowClosing()).
I have the options to exit, minimize or cancel.
How can I close the option pane on selecting 'cancel' without closing the entire application?
Object[]options = {"Minimize", "Exit","Cancel"};
int selection = JOptionPane.showOptionDialog(
null, "Please select option", "Options", 0,
JOptionPane.INFORMATION_MESSAGE, null, options, options[1]);
System.out.println(selection);
switch(selection)
{
case 2:
{
// do something
}
}
You can call yourFrame.setDefaultCloseOperation(WindowConstants.DO_NOTHING_ON_CLOSE); inside your windowClosing() method, if user chooses "cancel"....
If (selection == JOptionPane.CANCEL_OPTION)
{
// DO your stuff related to cancel click event.
}
The Oracle documentation gives a tips :
final JDialog dialog = new JDialog(frame,
"Click a button",
true);
dialog.setContentPane(optionPane);
dialog.setDefaultCloseOperation(
JDialog.DO_NOTHING_ON_CLOSE);
dialog.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent we) {
setLabel("Thwarted user attempt to close window.");
}
});
optionPane.addPropertyChangeListener(
new PropertyChangeListener() {
public void propertyChange(PropertyChangeEvent e) {
String prop = e.getPropertyName();
if (dialog.isVisible()
&& (e.getSource() == optionPane)
&& (prop.equals(JOptionPane.VALUE_PROPERTY))) {
//If you were going to check something
//before closing the window, you'd do
//it here.
dialog.setVisible(false);
}
}
});
dialog.pack();
dialog.setVisible(true);
int value = ((Integer)optionPane.getValue()).intValue();
if (value == JOptionPane.YES_OPTION) {
setLabel("Good.");
} else if (value == JOptionPane.NO_OPTION) {
setLabel("Try using the window decorations "
+ "to close the non-auto-closing dialog. "
+ "You can't!");
}
You have to remove default close operation and add your own listener, then use setVisible(false) to close it.
I would like to disable the close x in the upper left corner of my JOptionPane how would I do this?
Michael,
I don't know how to disable the Close[x] button. Alternatively, you can do nothing when user clicks on it. Check the code below:
JOptionPane pane = new JOptionPane("message");
JDialog dialog = pane.createDialog(null, "Title");
dialog.setDefaultCloseOperation(WindowConstants.DO_NOTHING_ON_CLOSE);
dialog.setVisible(true);
Is it reasonable for you?
you can overwrite your exit button by a cancel button declared in JOptionPane and handle your cancellation operation accordingly:
JOptionPane optionPane= new JOptionPane("message", JOptionPane.OK_CANCEL_OPTION);
final JDialog dialog = optionPane.createDialog(null, "Input");
dialog.setDefaultCloseOperation(JDialog.DO_NOTHING_ON_CLOSE);
dialog.addWindowListener(new WindowAdapter() {
#Override public void windowClosing(WindowEvent e) {
optionPane.setValue(JOptionPane.CANCEL_OPTION);
}
});
if (JOptionPane.CANCEL_OPTION!= ((Integer) optionPane.getValue()).intValue())
throw new myCancellationException();
You could always just show the dialog again when the user tries to close it without selecting an option. There's an example of how to override the default closing behavior at sun.com. Look under "Stopping Automatic Dialog Closing" and they have the following code:
final JOptionPane optionPane = new JOptionPane(
"The only way to close this dialog is by\n"
+ "pressing one of the following buttons.\n"
+ "Do you understand?",
JOptionPane.QUESTION_MESSAGE,
JOptionPane.YES_NO_OPTION);
final JDialog dialog = new JDialog(frame,
"Click a button",
true);
dialog.setContentPane(optionPane);
dialog.setDefaultCloseOperation(
JDialog.DO_NOTHING_ON_CLOSE);
dialog.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent we) {
setLabel("Thwarted user attempt to close window.");
}
});
optionPane.addPropertyChangeListener(
new PropertyChangeListener() {
public void propertyChange(PropertyChangeEvent e) {
String prop = e.getPropertyName();
if (dialog.isVisible()
&& (e.getSource() == optionPane)
&& (prop.equals(JOptionPane.VALUE_PROPERTY))) {
//If you were going to check something
//before closing the window, you'd do
//it here.
dialog.setVisible(false);
}
}
});
dialog.pack();
dialog.setVisible(true);
int value = ((Integer)optionPane.getValue()).intValue();
if (value == JOptionPane.YES_OPTION) {
setLabel("Good.");
} else if (value == JOptionPane.NO_OPTION) {
setLabel("Try using the window decorations "
+ "to close the non-auto-closing dialog. "
+ "You can't!");
}
Using that code, you could easily adapt the commented section to only allow the window to be closed when the user has clicked one of the available options and not the close button.
I'm not sure if there is a way to do this in JOptionPane.
Usually when people want more flexibility than JOptionPane offers (it's basically a bunch of static factories for a few dialogs), they write their own dialogs using JDialog.
JDialog offers the inherited method setUndecorated, which eliminates the X altogether. It's more work but you can make your dialog look however you want.