- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How can we set Mnemonic Key Radio Button in Java?
Mnemonic key is set so that a user can use Keyboard keys to select a Radio Button. For example, a key can be set with ALT −
radio2.setMnemonic(KeyEvent.VK_R);
Above, we have set key ALT+R for radio2.
The following is an example to set Mnemonic key radio button −
package my; import java.awt.FlowLayout; import java.awt.event.KeyEvent; import javax.swing.ButtonGroup; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JRadioButton; public class SwingDemo { public static void main(String[] args) { JRadioButton radio1 = new JRadioButton("Male"); JRadioButton radio2 = new JRadioButton("Female"); radio2.setMnemonic(KeyEvent.VK_R); ButtonGroup group = new ButtonGroup(); group.add(radio1); group.add(radio2); radio1.setSelected(true); JFrame frame = new JFrame(); frame.setLayout(new FlowLayout()); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.add(new JLabel("Select Gender:")); frame.add(radio1); frame.add(radio2); frame.pack(); frame.setVisible(true); } }
The output is as follows. When you will press Alt+R, you will be able to select the radio buttons.
Output
- Related Articles
- How to set Mnemonic key for selection of each JCheckBox in Java?
- How can we set the shortcut key to a JButton in Java?
- How to set OnclickListener on a radio button in android?
- How can I send radio button value in PHP using JavaScript?
- Can we disable JComboBox arrow button in Java?
- How to use Radio button in Android?
- How can I know which radio button is selected via jQuery?
- How can I know which radio button is selected via jQuery?
- How can we disable the maximize button of a JFrame in Java?
- How can we convert list to Set in Java?
- Bootstrap Form Radio Button
- How to use Radio Button in Android Kotlin?
- How can we set PRIMARY KEY on multiple columns of a MySQL table?
- How can we create an unmodifiable Set in Java 9?
- How to add radio button list in alert dialog?

Advertisements