Add Mnemonic Key for JRadioButton in Java
Description
The following code shows how to add Mnemonic Key for JRadioButton.
Example
/*from www.j av a 2s . co m*/
import java.awt.GridLayout;
import java.awt.event.KeyEvent;
import javax.swing.ButtonGroup;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
public class Main {
public static void main(String args[]) {
JFrame frame = new JFrame("Grouping Example");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel panel = new JPanel(new GridLayout(0, 1));
ButtonGroup group = new ButtonGroup();
JRadioButton aRadioButton = new JRadioButton("A");
JRadioButton bRadioButton = new JRadioButton("B");
bRadioButton.setMnemonic(KeyEvent.VK_B);
panel.add(aRadioButton);
group.add(aRadioButton);
panel.add(bRadioButton);
group.add(bRadioButton);
frame.add(panel);
frame.setSize(300, 200);
frame.setVisible(true);
}
}
The code above generates the following result.
Home »
Java Tutorial »
Swing »
Java Tutorial »
Swing »