

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Java Program to disable the first item on a JComboBox
To disable the first index, on the click of a button, let us implement for index > 0 i.e. item 2:
System.out.println("Index 0 (First Item) is disabled... "); comboBox.addItemListener(e -> { if (comboBox.getSelectedIndex() > 0) { System.out.println("Index = " + comboBox.getSelectedIndex()); } });
By implementing above, we have disabled the first item.
The following is an example to disable the first item on a JComboBox:
Example
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); frame.setLayout(null); frame.setSize(400,500); frame.setVisible(true); System.out.println("Index 0 (First Item) is disabled... "); comboBox.addItemListener(e -> { if (comboBox.getSelectedIndex() > 0) { System.out.println("Index = " + comboBox.getSelectedIndex()); } }); } public static void main(String[] args) { new SwingDemo(); } }
The output is as follows. You can now select all the indexes except index 0 (first item). Let us select the first index i.e. the second item:
Output
Meanwhile, while selecting Tennis above, the following is displayed on Console:
- Related Questions & Answers
- Java Program to select the first item in JList
- Can we disable JComboBox arrow button in Java?
- How to display the first element in a JComboBox in Java
- How to pre-select JComboBox item by index in Java?
- Disable a dropdown item with Bootstrap
- How to disable a menu item in JavaFX
- Java Program to set JComboBox in JOptionPane
- How to move specific item in array list to the first item in Java?
- How to add items in a JComboBox on runtime in Java
- Disable a list item in a Bootstrap list group
- How to disable close button on a JFrame in Java?
- How to display the items in a JComboBox in Java
- How to center align the items of a JComboBox in Java?
- How can we show a popup menu when the user right-clicks on a JComboBox in Java?
- How to display the different font items inside a JComboBox in Java?
Advertisements