- 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 set the shortcut key to a JCheckBox in Java?
A JCheckBox is a subclass of JToggleButton and it can be a small box that is either checked or unchecked. When we click on a JCheckBox, it changes from checked to unchecked or vice versa automatically. A JCheckBox can generate an ActionListener or ItemListener whenever the checkbox is changed. We can set the shortcut keys to a JCheckBox by using the setMnemonic() method.
Example
import java.awt.*; import java.awt.event.*; import javax.swing.*; public class JCheckBoxShortCutKeyTest extends JFrame { private JCheckBox checkBox; public JCheckBoxShortCutKeyTest() { setTitle("JCheckBoxShortCutKey Test"); checkBox = new JCheckBox("Check or Press ALT-C"); checkBox.setBorder(BorderFactory.createLineBorder(Color.lightGray)); checkBox.setMnemonic('C'); add(checkBox, BorderLayout.CENTER); checkBox.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent evt) { JOptionPane.showMessageDialog(null, "A Checkbox checked or pressed"); } }); setSize(400, 300); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setLocationRelativeTo(null); setVisible(true); } public static void main(String args[]) throws Exception { new JCheckBoxShortCutKeyTest(); } }
Output
- Related Articles
- How can we set the shortcut key to a JButton in Java?
- How to set Mnemonic key for selection of each JCheckBox in Java?
- How to set tooltip text for JCheckBox in Java?
- How can we set a border to JCheckBox in Java?\n
- How to get or set the selection state of JCheckBox in Java
- Create a shortcut key to activate an element in HTML
- How to disable JCheckBox if not checked in Java
- How to hide and display JCombobox with a JCheckBox in Java?
- How to Copy Cell Above or Cell Left with Shortcut Key in Excel?
- How to select one item at a time from JCheckBox in Java?
- How to create a Default Cell Editor that uses a JCheckBox in Java?
- Java Program to create JCheckBox from text in Swing
- How can we set Mnemonic Key Radio Button in Java?
- How to go back to the previous/last sheet with a shortcut in Excel?
- How to use Iterator to loop through the Map key set?

Advertisements