JButton unresponsive to actionListener

Go To StackoverFlow.com

0

I've trouble with actionListener. I created own simple dialog, which has only two JButtons - Yes and No. When I click on button, actionListener doesn't respond.

This is my code:

private void showInfoNewUML() {        
    Dimension buttonsSize = new Dimension(60, 25);
    Dimension programSize = new Dimension(1200, 700);
    final JDialog dialogWindow = new JDialog(this, "Erase actual UML diagram"
            + " with his files", true);        
    JTextArea descDialogWindow = new JTextArea("Do you really erase actual\n"
            + "UML diagram with his files?   ");
    descDialogWindow.setEditable(false);
    descDialogWindow.setBackground(new Color(220, 220, 220));
    descDialogWindow.setBorder(null);
    dialogWindow.getContentPane().setBackground(new Color(220, 220, 220));
    dialogWindow.setDefaultCloseOperation(DISPOSE_ON_CLOSE);
    dialogWindow.setModal(true);
    dialogWindow.setResizable(false);
    dialogWindow.setLayout(new FlowLayout());
    dialogWindow.setSize(310, 100);
    dialogWindow.setLocation((int) programSize.getWidth() / 2,
            (int) programSize.getHeight() / 2);
    JButton buttonYes = new JButton("Yes");
    JButton buttonNo = new JButton("No");
    buttonYes.setPreferredSize(buttonsSize);
    buttonNo.setPreferredSize(buttonsSize);
    dialogWindow.add(descDialogWindow);
    dialogWindow.add(buttonYes);
    dialogWindow.add(buttonNo);
    dialogWindow.setVisible(true);

    buttonYes.addActionListener(new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent e) {
            buttonAnoActionPerformed(e);
        }

        private void buttonAnoActionPerformed(ActionEvent e) {
            dialogWindow.setVisible(false);
        }
    });

    buttonNo.addActionListener(new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent e) {
            buttonNeActionPerformed(e);
        }

        private void buttonNeActionPerformed(ActionEvent e) {
            dialogWindow.setVisible(false);
        }
    });
}

I would like close this dialog after I click on button. When I click to top right button with cross, dialog window closes.

Thank you for help with this trouble.

2012-04-05 21:37
by avalagne
Creating an SSCCE will help us understand your problem - Jeffrey 2012-04-05 21:38
"Do you really erase actual\n" + "UML diagram with his files?" It seems this would be better suited to <code>JOptionPane.showConfirmDialog(...)</code> (and overloaded variants) - Andrew Thompson 2012-04-05 21:44
Yeah, I would like to do. Unfortunately, I'm doing an application in the Czech language, which is adjustable in response to showConfirmDialog Yes [in czech "Ano"] and No [in czech "Ne"]. This is reason why I create own dialog - avalagne 2012-04-05 21:48
@avalagne you can customize the button labels in a JOptionPane. See for example the "Yes, please" and "No, thanks" dialog a bit of the way down the page @ http://docs.oracle.com/javase/tutorial/uiswing/components/dialog.htm - Mike Clark 2012-04-05 21:54
Thank you so much... This is great advice. Helpful link! Actually, I read this page, but I have not read everything on this page. Thank you again - avalagne 2012-04-05 22:05


2

Try adding the ActionListeners before calling dialogWindow.setVisible(true);.

Your dialog is modal, and so showInfoNewUML will block at dialogWindow.setVisible(true); until after the dialog is closed, which is too late to register any useful listeners.

2012-04-05 21:44
by Mike Clark
Holy s**t. Thank you sir! I was looking for a problem somewhere else - avalagne 2012-04-05 21:53
Ads