- 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 to add JRadioButtonMenuItem in Java
Let’s say we have a menu and within that we need to set the JRadioButtonMenuItem. Here’s the Menu −
JMenu editMenu = new JMenu("Edit"); editMenu.setMnemonic(KeyEvent.VK_E); menuBar.add(editMenu);
Now, set the editMenu and add it to the MenuBar −
JMenu editMenu = new JMenu("Edit"); editMenu.setMnemonic(KeyEvent.VK_E); menuBar.add(editMenu);
Now, create Options Menu and add it to the editMenu −
JMenu optionsMenu = new JMenu("Options"); optionsMenu.setMnemonic(KeyEvent.VK_O); editMenu.add(optionsMenu);
Now, set the JRadioButtonMenuItem and add it to the editMenu created above −
ButtonGroup group = new ButtonGroup(); JRadioButtonMenuItem radioMenuItem = new JRadioButtonMenuItem("SmartInsertMode", true); radioMenuItem.setMnemonic(KeyEvent.VK_I); optionsMenu.add(radioMenuItem); group.add(radioMenuItem); JRadioButtonMenuItem radioMenuItem2 = new JRadioButtonMenuItem("SmartDeleteMode"); radioMenuItem.setMnemonic(KeyEvent.VK_D); optionsMenu.add(radioMenuItem2); group.add(radioMenuItem2);
The following is an example add JRadioButtonMenuItem in Java −
Example
package my; import java.awt.Color; import java.awt.event.KeyEvent; import javax.swing.ButtonGroup; import javax.swing.JCheckBoxMenuItem; import javax.swing.JFrame; import javax.swing.JMenu; import javax.swing.JMenuBar; import javax.swing.JMenuItem; import javax.swing.JRadioButtonMenuItem; import javax.swing.UIManager; public class SwingDemo { public static void main(final String args[]) { JFrame frame = new JFrame("MenuBar Demo"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); JMenuBar menuBar = new JMenuBar(); UIManager.put("MenuBar.background", Color.ORANGE); JMenu fileMenu = new JMenu("File"); fileMenu.setMnemonic(KeyEvent.VK_F); menuBar.add(fileMenu); JMenuItem menuItem1 = new JMenuItem("New", KeyEvent.VK_N); fileMenu.add(menuItem1); JMenuItem menuItem2 = new JMenuItem("Open File", KeyEvent.VK_O); fileMenu.add(menuItem2); menuItem2.setEnabled(false); JMenu editMenu = new JMenu("Edit"); editMenu.setMnemonic(KeyEvent.VK_E); menuBar.add(editMenu); JMenuItem menuItem3 = new JMenuItem("Cut", KeyEvent.VK_C); editMenu.add(menuItem3); JMenu searchMenu = new JMenu("Search"); searchMenu.setMnemonic(KeyEvent.VK_S); menuBar.add(searchMenu); JMenu projectMenu = new JMenu("Project"); projectMenu.setMnemonic(KeyEvent.VK_P); menuBar.add(projectMenu); JMenu runMenu = new JMenu("Run"); runMenu.setMnemonic(KeyEvent.VK_R); menuBar.add(runMenu); JCheckBoxMenuItem checkMenuItem = new JCheckBoxMenuItem("SelectAll"); checkMenuItem.setMnemonic(KeyEvent.VK_B); editMenu.add(checkMenuItem); JMenu optionsMenu = new JMenu("Options"); optionsMenu.setMnemonic(KeyEvent.VK_O); editMenu.add(optionsMenu); ButtonGroup group = new ButtonGroup(); JRadioButtonMenuItem radioMenuItem = new JRadioButtonMenuItem("SmartInsertMode", true); radioMenuItem.setMnemonic(KeyEvent.VK_I); optionsMenu.add(radioMenuItem); group.add(radioMenuItem); JRadioButtonMenuItem radioMenuItem2 = new JRadioButtonMenuItem("SmartDeleteMode"); radioMenuItem.setMnemonic(KeyEvent.VK_D); optionsMenu.add(radioMenuItem2); group.add(radioMenuItem2); frame.setJMenuBar(menuBar); frame.setSize(550, 350); frame.setVisible(true); } }
Output
- Related Articles
- How to display a JRadioButtonMenuItem in Java?\n
- How to add elements in Java CopyOnWriteArrayList?
- How to add elements in Java ArrayBlockingQueue?
- How to add tooltip to JLabel in Java?
- How to add ScrollBar to JList in Java?
- How to add Icon to JButton in Java?
- How to add JList to Scroll pane in Java?
- How to add action listener to JButton in Java
- How to add background Image to JFrame in Java
- How to add line border to JLabel in Java?
- How to add elements to AbstractCollection class in Java?
- How to add elements to AbstractList class in Java?
- How to add empty border to JPanel in Java?
- How to add JTable to Panel in Java Swing?
- How to add different comments in the Java code?

Advertisements