Java Swing GUI

JComboBox – Java Swing – Example

In this tutorial, we are going to see an example of JComboBox in Java Swing. JComboBox is part of the Java Swing package. JComboBox inherits from the JComponent class. JComboBox displays a contextual menu as a list that allows the user to select an option from the specified list. JComboBox can be editable or read-only according to the programmer’s choice.


 
 

JComboBox constructors class:
JComboBox constructors
Description
JComboBox() Creates a new empty JComboBox.
JComboBox(ComboBoxModel M) Create a new JComboBox with the elements of the specified ComboBoxModel
JComboBox(E[] i) Creates a new JComboBox with the elements of the specified array.
JComboBox(Vector items) Creates a new JComboBox with the elements of the specified vector.

 

Commonly used methods:
  • addItem(E item) : Adds the element to JComboBox
  • addItemListener( ItemListener l) : Adds an ItemListener to JComboBox.
  • getItemAt(int i) : Returns the element at index i
  • getItemCount() : Returns the number of elements in the list
  • getSelectedItem() : Returns the element that is selected
  • removeItemAt(int i) : Deletes the element at index i
  • setEditable(boolean b) : The boolean b determines if the list is modifiable or not. If the value “true” is transmitted, the list is modifiable, if “false” the list is not modifiable.
  • setSelectedIndex(int i) : Selects the JComboBox element at index i.
  • showPopup() : Allows the drop-down list to display its pop-up window.
  • setEnabled(boolean b) : Activates the drop-down list so that the items can be selected.
  • removeItem(Object anObject) : Removes an element from the list of elements.
  • removeAllItems() : Deletes all the elements of the list.
  • removeActionListener(ActionListener l) : Deletes ActionListener.
  • isPopupVisible() : Determines popup visibility.
  • getItemCount() : Returns the number of elements in the list.
 

Example of JComboBox in Java Swing:
import javax.swing.*; 
import java.awt.*; 
import java.awt.event.*; 

class ComboBoxExample extends JFrame implements ItemListener { 

    // frame 
    static JFrame frame; 
    // combobox 
    static JComboBox combobox; 
    // label 
    static JLabel l1, l2; 

    public static void main(String[] args) 
    {
        // create a new frame 
        frame = new JFrame("frame"); 
  
        // create an object
        ComboBoxExample obj = new ComboBoxExample(); 
  
        // set the layout of the frame 
        frame.setLayout(new FlowLayout()); 
  
        // array of strings containing languages
        String s1[] = { "Java", "PHP", "Python", "C++", "Ruby" }; 
  
        // create a checkbox
        combobox = new JComboBox(s1); 
  
        // add ItemListener
        combobox.addItemListener(obj); 
  
        // create labels
        l1 = new JLabel("What is your favorite language? "); 
        l2 = new JLabel("[Java]"); 
  
        // set the text color
        l2.setForeground(Color.blue); 
  
        // create a new panel
        JPanel p = new JPanel(); 

        // add combobox and labels to the panel
        p.add(l1);
        p.add(combobox); 
        p.add(l2); 

        // add panel to frame
        frame.add(p); 

        // set the frame size 
        frame.setSize(400, 200); 

        frame.show(); 
    } 
    public void itemStateChanged(ItemEvent e) 
    { 
        // check if the state of the combobox is changed 
        if (e.getSource() == combobox) { 
  
            l2.setText(" ["+combobox.getSelectedItem()+"]"); 
        } 
    } 
}

Output:


mcqMCQPractice competitive and technical Multiple Choice Questions and Answers (MCQs) with simple and logical explanations to prepare for tests and interviews.Read More

Leave a Reply

Your email address will not be published. Required fields are marked *