I'm having a problem with dialog box basics, and i have looked this up, but i have found nothing on it. What is the JOptionPane for the X in the top right of the GUI? When i click it, it carries on running the script. i know that it requires an `System.exit(0);
This is what i have so far:
int dialogButton= JOptionPane.YES_NO_CANCEL_OPTION;
int dialogResult = JOptionPane.showConfirmDialog(null, "", "Info",dialogButton);
if(dialogResult==JOptionPane.YES_OPTION){
//Code for YES Button Clicked
} else if(dialogResult == JOptionPane.NO_OPTION) {
System.exit(0);
} else if(dialogResult == JOptionPane.CANCEL_OPTION) {
System.exit(0);
}
JOptionPane pane = new JOptionPane("message");
JDialog dialog = pane.createDialog(null, "Title");
dialog.setDefaultCloseOperation(WindowConstants.DO_NOTHING_ON_CLOSE);
dialog.show();
You can set the default close operation of the dialog box.
Related
For my CS class they require us to use JavaFX alerts. I can make an alert appear, but how do I get what button was clicked? What would be the best way to go about getting this data?
Also if possible, I'd like to make it have a drop down panel and when the user selects and option the alert closes and prints what the user selected.
Here's some example code that I have. When I click one of the buttons, it just closes the dialog.
Alert a = new Alert(AlertType.NONE, "Promote pawn to:", new ButtonType("Queen"), new ButtonType("Rook"));
a.setTitle("Title");
a.setHeaderText("My header text");
a.setResizable(true);
a.setContentText("Content text");
a.showAndWait();
Thanks,
You can do
ButtonType queen = new ButtonType("Queen");
ButtonType rook = new ButtonType("Rook");
Alert a = new Alert(AlertType.NONE, "Promote pawn to:", queen, rook);
a.setTitle("Title");
a.setHeaderText("My header text");
a.setResizable(true);
a.setContentText("Content text");
a.showAndWait().ifPresent(response -> {
if (response == queen) {
// promote to queen...
} else if (response == rook) {
// promote to rook...
}
});
İf you want to do it with Non-Blocking type of code:
final Alert alert2 = new Alert(Alert.AlertType.CONFIRMATION);
alert2.show();
alert2.setOnCloseRequest(new EventHandler<DialogEvent>() {
#Override
public void handle(DialogEvent event) {
ButtonType result=alert2.getResult();
String resultText=result.getText();
//result logic
}
});
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);
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.