Hi! Please, tell me how to make the JDialog close button working in a proper way. Now it does not work. System.out.println(value); produces 0. But if I write if (value == 0) , then nothing happens. Any ideas? Thanks!

public static void about() {
        final JOptionPane optionPane = new JOptionPane(
                "Some info",
                JOptionPane.PLAIN_MESSAGE,
                JOptionPane.CLOSED_OPTION);
        final JDialog dialog = new JDialog((JDialog)null,"Hello",true);
        dialog.setContentPane(optionPane);
        dialog.setDefaultCloseOperation(
        JDialog.DISPOSE_ON_CLOSE);
        dialog.pack();
        final Toolkit toolkit = Toolkit.getDefaultToolkit();
        final Dimension screenSize = toolkit.getScreenSize();
        final int x = (screenSize.width - dialog.getWidth()) / 2;
        final int y = (screenSize.height - dialog.getHeight()) / 2;
        dialog.setLocation(x, y);
        dialog.setVisible(true);
        int value = ((Integer)optionPane.getValue()).intValue();
        System.out.println(value);
        if (value == JOptionPane.CLOSED_OPTION) {
            dialog.dispose();
        }
    }

Have you tried: dialog.setVisible(false) ?

Why are you passing in JOptionPane.CLOSED_OPTION? That should be a return value, shouldn't it? It's probably got the same value as the input option you're looking for (per the API, you want an option type:
optionType - the options to display in the pane: DEFAULT_OPTION, YES_NO_OPTION, YES_NO_CANCEL_OPTION, OK_CANCEL_OPTION)

That won't be the problem you're having, but it indicates that you're flailing a bit. Simple dialogs are usually simple. (um, yeah...) For example, here's a standard item in any GUI application I make:

public static void warn (String warning)
	{
		JOptionPane.showMessageDialog(frame, warning);
	}

Any class can complain at the user with this - pretty simple, huh?

If you want it to get a value, you might do something like:

public int openOrCreateWorkspace( )
	{
		Object[] options = {"Open Workspace", "Create Workspace"};
		int response = JOptionPane.showOptionDialog(null, 
				"Open a workspace or create a new workspace?",
			    "workspace",
			    JOptionPane.YES_NO_OPTION,
			    JOptionPane.QUESTION_MESSAGE,
			    null,     
			    options,  
			    options[0]); 
		return response;
	}

(I don't mind giving you the literal code here, since this is directly from the Swing tutorial, just tweaked to the thing I'm working on right now)
I think you might be complicating things a little more than you need to with all that code - none of it should be be needed. If you want to build more complicated dialog, yes, you'll have to get fancy - a multi-pane dialog that returns mutiple values, or a set of nested dialogs, say - but for what you're doing I think there are easier ways built in.

commented: helpful +14

Yeah, you are right. I've overcomplicated the task. In fact, I needed just a simple dialog with the "Ok" button. Thanks!

JOptionPane.showMessageDialog(null,
                "Some info",
                "About",
                JOptionPane.PLAIN_MESSAGE);
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.