Hi! I am developing a pos for a store. I have created a settings window. On this window there is a button which is used to set a name for store. When user press this button a message dialog appears with a text field and a OK button so that user enter text for his store name. I made this message dialog by using a jpanel. Below I am giving the code. for this message dialog.

JTextField field = new JTextField(20);
    JLabel label = new JLabel("Enter name for your store");
    JPanel p = new JPanel(new BorderLayout(5,2));
    p.add(field);        JOptionPane.showMessageDialog(null,p,"Store Name required",JOptionPane.PLAIN_MESSAGE,null);
    String storeName = field.getText();
    try {
    } catch (SQLException ex) {
        Logger.getLogger(Settings.class.getName()).log(Level.SEVERE, null, ex);

My code is running perfectly. When user enters a name for his store and presses Ok button,the store name is saved in database perfectly.
But the problem is that when user want to close this message dialog and click on the 'X' button at top right corner of this message dialog, the dialog closes and it again save the store name in database i-e it again executes the code which runs for OK button.
I want that on closing the message dialog , the dialog should only close

2 Weeks
Discussion Span
Last Post by Saboor Siddique

Your code says:

create some fields
open a (modal) message dialog
...  (modal dialog waits for user to click OK or close etc)
set store name

so that's why it sets the store name when you close the dialog

If you only want it to update on the save button, then you don't want the code at lines 6-11 above, you just want that code in the save button's event handler (if I understood you correctly).
OR Just use the standard dialog that has an input field !

Edited by JamesCherrill


My this code is in event handler of a button named 'set store name'. When user presses this button then my code executes and creates and displays a message dialog with
Text field, a label and on a jpanel.
Before this i used simple input dialog . as you know input dialog appears with 2 buttons ' ok' button and a 'cancel' button. On pressing ok button store name is saved in database which is correct behaviour. But on pressing cancel button store name agian saves in database which is incorrect behavior. On pressing cancel button, input dialog should just close. So to get rid of that cancel button i made my custom message dialog whose code you have seen above.
Is there any way that cancel button of input dialog behaves in only way i want i-e just close the input dialog


Your mistake was to use a message dialog. That's intended to show a message and then close. If you want the user to be able to chose between OK and cancel/close then you need to use another type of dialog.

You could use a confirm dialog with OK and Cancel buttons . That returns an int showing whenther it was OK, Cancel, or close that was clicked.

Even better, ditch your whole thing and just use an input dialog to prompt for a store and accept user input. If the user presses OK it returns the input. If the user presses Cancel or closes the dialog it returns a null. Just test that return value to decide whether to save or do nothing. This is the obvious "right" solution.

Edited by JamesCherrill

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.