I think you are missing the JOptionPane point. It comes with the ability to display its own dialog ...
public class TestOptionPane02 { public static void main(String[] args) { new TestOptionPane02(); } public TestOptionPane02() { EventQueue.invokeLater(new Runnable() { @Override public void run() { try { UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); } catch (ClassNotFoundException ex) { } catch (InstantiationException ex) { } catch (IllegalAccessException ex) { } catch (UnsupportedLookAndFeelException ex) { } JTextField textField = new JTextField(10); String btnString1 = "Save Object"; String btnString2 = "Delete Object";
To do this manually, you will need to do a little work.
First, you will need to listen for the panel property change events, look for changes in JOptionPane.VALUE_PROPERTY and ignore any JOptionPane.UNINITIALIZED_VALUE value ...
Once you find the change, you will need to delete your dialog.
You will need to retrieve the value that was selected using the JOptionPane#getValue , which returns an Object . You will have to interrupt the meaning of this value yourself ...
Needless to say, the JOptionPane.showXxxDialog do all this for you ...
Now, if you are worried about having to go through all the dialog settings, I would write a utility method that completely or completely fulfilled the required parameters ... but that's just me
UPDATED
I donโt know why I didnโt think about it before ...
Instead of passing the String array as the options parameter, pass the JButton array. This way you can join your listeners.
options - an array of objects indicating the possible choices the user can make; if the objects are components, they are displayed correctly ; objects other than String are rendered using toString methods; if this parameter is null, the parameters are determined using the Look and Feel function