JRootPane: setDefaultButton(JButton defaultButton)
import java.awt.Dimension;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JPanel;
public class Main extends JPanel {
public static void main(String[] a) {
JDialog f = new JDialog();
f.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
JButton btOK = new JButton("Press Enter to click me, I am the default.");
btOK.setToolTipText("Save and exit");
f.getRootPane().setDefaultButton(btOK);
JPanel p = new JPanel();
p.add(btOK);
p.add(new JButton("I am NOT the default."));
f.getContentPane().add(p);
f.pack();
f.setSize(new Dimension(300, 200));
f.setVisible(true);
}
}
Related examples in the same category