- 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 hide and display JCombobox with a JCheckBox in Java?
To toggle visibility with JCheckBox, use isVisible() method:
JCheckBox toggleVisibility = new JCheckBox("Hide/Show"); toggleVisibility.setSelected(comboBox.isVisible()); toggleVisibility.addItemListener(e -> { comboBox.setVisible(e.getStateChange() == ItemEvent.SELECTED); });
The following is an example to hide and display JCombobox with a JCheckBox in Java:
Example
import java.awt.BorderLayout; import java.awt.event.ItemEvent; import javax.swing.*; public class SwingDemo { JFrame frame; SwingDemo(){ frame = new JFrame("ComboBox"); String Sports[]={"Select","Tennis","Cricket","Football"}; JComboBox comboBox = new JComboBox(Sports); comboBox.setBounds(50, 50,90,20); frame.add(comboBox, BorderLayout.CENTER); JCheckBox toggleVisibility = new JCheckBox("Hide/Show"); toggleVisibility.setSelected(comboBox.isVisible()); toggleVisibility.addItemListener(e -> { comboBox.setVisible(e.getStateChange() == ItemEvent.SELECTED); }); frame.add(toggleVisibility, BorderLayout.SOUTH); //frame.setLayout(null); frame.setSize(600,100); frame.setVisible(true); } public static void main(String[] args) { new SwingDemo(); } }
The output is as follows. By default, the following JComboBox is visible:
Output
Now, uncheck on “Hide/Show” above to hide the JComboBox. On uncheck, the JComboBox will hide as shown below:
- Related Articles
- How to display the items in a JComboBox in Java
- How to display the first element in a JComboBox in Java
- How to display the different font items inside a JComboBox in Java?
- How to set the shortcut key to a JCheckBox in Java?
- Handle JCheckBox Events with an ItemListener in Java
- How can we set a border to JCheckBox in Java?\n
- How to add items in a JComboBox on runtime in Java
- How to disable JCheckBox if not checked in Java
- How to set tooltip text for JCheckBox in Java?
- How to hide or display cells with zero values in selected ranges in Excel?
- How to center align the items of a JComboBox in Java?
- How to handle action event for JComboBox in Java?
- 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?
- How to create a Default Cell Editor that uses a JComboBox in Java?

Advertisements