I want to add a button (JButton) at the bottom of a JDialog which should close the JDialog when pressed. The problem is I don\'t know what to write in the ActionListener of
In addition to other answers, you can set it as the default button for the dialog root pane:
JButton myButton = new JButton("Close");
myButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
dispose(); // Or whatever else
setVisible(false);
}
});
getRootPane().setDefaultButton(myButton);
That way, its ActionListener will be called whenever Enter is pressed.