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.
"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
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.