ActionListener on JOptionPane

自闭症网瘾萝莉.ら 提交于 2019-11-28 13:53:47

I think you're missing the point of the JOptionPane. It comes with the ability to show it's 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";

                //Create an array of the text and components to be displayed.
                String msgString1 = "Object label:";
                Object[] array = {msgString1, textField};
                //Create an array specifying the number of dialog buttons
                //and their text.
                Object[] options = {btnString1, btnString2};

                int result = JOptionPane.showOptionDialog(null, array, "", JOptionPane.YES_NO_OPTION, JOptionPane.PLAIN_MESSAGE, "New Object", options, options[0]);
                switch (result) {
                    case 0:
                        System.out.println("Save me");
                        break;
                    case 1:
                        System.out.println("Delete me");
                        break;
                }
            }
        });
    }
}

To do it manually, you're going to have to do a little more work.

Firstly, you're going to have to listen to the panel's property change events, looking for changes to the JOptionPane.VALUE_PROPERTY and ignoring any value of JOptionPane.UNINITIALIZED_VALUE...

Once you detect the change, you will need to dispose of your dialog.

The you will need extract the value that was selected via the JOptionPane#getValue method, which returns an Object. You will have to interrupt the meaning to that value yourself...

Needless to say, JOptionPane.showXxxDialog methods do all this for you...

Now if you worried about having to go through all the setup of the dialog, I'd write a utility method that either did it completely or took the required parameters...but that's just me

UPDATED

Don't know why I didn't think it sooner...

Instead of passing an array of String as the options parameter, pass an array of JButton. This way you can attach your own listeners.

options - an array of objects indicating the possible choices the user can make; if the objects are components, they are rendered properly; non-String objects are rendered using their toString methods; if this parameter is null, the options are determined by the Look and Feel

For the flexibility you seem to want you should have your class extend JFrame instead of JDialog. Then declare your buttons as JButtons: JButton saveButton = new JButton("Save"); and add an actionListnener to this button: saveButton.addActionListener(); either you can put a class name inside the parenthesis of the saveButton, or you can simply pass it the keyword 'this' and declare a method called actionPerformed to encapsulate the code that should execute when the the button is pressed. See this link for a JButton tutorial with more details: http://docs.oracle.com/javase/tutorial/uiswing/events/actionlistener.html

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!