Set the default button in Java
Description
The following code shows how to set the default button.
Example
Pressing the Enter key to trigger button's event.
/*from w w w . j av a 2 s . c om*/
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JRootPane;
public class Main {
public static void main(String args[]) {
JFrame frame = new JFrame("DefaultButton");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JButton button4 = new JButton("AAA");
frame.add(button4);
frame.add(new JButton("BBB"));
JRootPane rootPane = frame.getRootPane();
rootPane.setDefaultButton(button4);
frame.setSize(300, 200);
frame.setVisible(true);
}
}
The code above generates the following result.
Home »
Java Tutorial »
Swing »
Java Tutorial »
Swing »