Disabling the close x in a JOptionPane - java

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.

Related

JOptionpane + JDialog (non-modal) get the return value

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.

confirmation before press YES to exit program in Java

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);

Dialog box basics

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.

Closing A JOptionPane Programmatically

I am working on a project in which I would like to close a generic JOptionPane programmatically (by not physically clicking on any buttons). When a timer expires, I would like to close any possible JOptionPane that may be open and kick the user back to the login screen of my program. I can kick the user back just fine, but the JOptionPane remains unless I physically click a button on it.
I have looked on many sites with no such luck. A doClick() method call on the "Red X" of the JOptionPane does not seem possible, and using JOptionpane.getRootFrame().dispose() does not work.
Technically, you can loop through all windows of the application, check is they are of type JDialog and have a child of type JOptionPane, and dispose the dialog if so:
Action showOptionPane = new AbstractAction("show me pane!") {
#Override
public void actionPerformed(ActionEvent e) {
createCloseTimer(3).start();
JOptionPane.showMessageDialog((Component) e.getSource(), "nothing to do!");
}
private Timer createCloseTimer(int seconds) {
ActionListener close = new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
Window[] windows = Window.getWindows();
for (Window window : windows) {
if (window instanceof JDialog) {
JDialog dialog = (JDialog) window;
if (dialog.getContentPane().getComponentCount() == 1
&& dialog.getContentPane().getComponent(0) instanceof JOptionPane){
dialog.dispose();
}
}
}
}
};
Timer t = new Timer(seconds * 1000, close);
t.setRepeats(false);
return t;
}
};
This code gotten from
https://amp.reddit.com/r/javahelp/comments/36dv3t/how_to_close_this_joptionpane_using_code/ seems to be the best approach to me. It involves Instantiating the JOptionPane class rather that using the static helper methods to do it for you. The benefit is you have a JOptionPane object that you can dispose when you want to close the dialog.
JOptionPane jop = new JOptionPane();
jop.setMessageType(JOptionPane.PLAIN_MESSAGE);
jop.setMessage("Hello World");
JDialog dialog = jop.createDialog(null, "Message");
// Set a 2 second timer
new Thread(new Runnable() {
#Override
public void run() {
try {
Thread.sleep(2000);
} catch (Exception e) {
}
dialog.dispose();
}
}).start();
dialog.setVisible(true);

Cancel an option pane

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.

Categories