Fake actionEvent doesn't dispose window. (Password dialog box) - java

I am using JOptionPane confirm dialog for password entry, it has OK and Cancel buttons and when I am using a while loop to detect when correct password has been entered, when user presses Cancel or "X", I trigger fake actionEvent to dispose window using this.dispose(); but for some weird reason it doesn't work and window opens regardless.
Here is the code:
while(identifier<1) {
if(action == 0) {
if(Arrays.equals(password.getPassword(),pass)) {
break;
} else {
actionPerformed(new ActionEvent(2,-1,""));
break;
}
} else {
actionPerformed(new ActionEvent(2,-1,"");
break;
}
}
And here is the part from actionPerformed method which should close the window:
if(e.getSource()==2) {
this.dispose();
}

Related

Two Eventhandler for same Key

I have a JavaFX Scene with with a login form and a register form which slide into the scene from the outside. The main anchorpane is root and the other two are login_form and register_form. Each form has one button - login which is calling login() and register which is callling register().
login_trigger and register_trigger are two buttons which let the forms slide in. When the login form slides in, I should be able to open login() when I press Enter. The same for the register form.
That's what I have thought, but whenever I press Enter, nothing happens. It is working without a problem with the buttons but not with Enter. I would like to have the Eventhandler set to the form which is displayed.
I thought it would work when I set the EventHandler to null, when the form is closing and to give it a value when it's opening. Is there another way to do it or do I have just a stupid mistake in my code?
login_trigger.setOnAction(e -> {
if (login_form.getTranslateX() != 0) {
openLogin.play();
login.setOnKeyPressed(e2 -> {
if (e2.getCode() == KeyCode.ENTER) {
login();
}
});
if (register_form.getTranslateX() != 1280) {
closeRegister.setToX(1280);
closeRegister.play();
}
} else {
login.setOnKeyPressed(null);
closeLogin.setToX(-250);
closeLogin.play();
}
});
register_trigger.setOnAction(e -> {
if (register_form.getTranslateX() != 1030) {
openRegister.play();
register.setOnKeyPressed(e2 -> {
if (e2.getCode() == KeyCode.ENTER) {
register();
}
});
if (login_form.getTranslateX() != -250) {
closeLogin.setToX(-250);
closeLogin.play();
}
} else {
register.setOnKeyPressed(null);
closeRegister.setToX(1280);
closeRegister.play();
}
});

Keeping a java application open when a user selects "Cancel"?

I have a very basic question.
I have the following WIndowClosing() event in java. I want to keep the application open if the user clicks "Cancel". How do I do this?
The application keeps closing when "Cancel" is selected.
This is what I have:
public void windowClosing(WindowEvent evt)
{
SAVE_MODE event = m_model.saveChanges();
if(event == SAVE_MODE.YES)
{
saveChanges();
System.exit(0);
}
else if(event == Common.SAVE_MODE.NO)
{
System.exit(0);
}
else
{
//Keep the application open -- how do I say that in code?
}
}
Note the following:
The application still closes when the ELSE clause is empty. I don't think that should be happening.
This should have taken me two minutes to solve. Any help would be appreciated. Thanks.
(if you want to credit me with the correct answer)
make sure you're calling [yourJFrame].setDefaultCloseOperation(DO_NOTHING_ON_CLOSE);
I think probably a different approach is all that's needed. Set your default close operation for the main frame (during your initial setup) to:
frame.setDefaultCloseOperation(WindowConstants.DO_NOTHING_ON_CLOSE);
Then add your windowClosing code to exit on OK, and do nothing on Cancel.
frame.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent evt) {
SAVE_MODE event = m_model.saveChanges();
switch (event)
{
case YES:
saveChanges();
System.exit(0);
break;
case NO:
System.exit(0);
break;
default:
break;
}
}
});

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.

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.

Disabling the close x in a JOptionPane

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.

Categories