Curriculum
Course: Learn Java Programming
Login

Curriculum

Learn Java Programming

Text lesson

Creating a JComboBox in Java Swings

In this lesson, you will learn.

  • Working with JComboBox
  • Constructors and Methods of JComboBox
  • Examples

 

What is a JComboBox

 

 

  1. A JComboBox represents a drop-down menu that allows users to choose one of several options.
  2. It can also be editable, meaning users can type a value directly into the field.

 

Constructors of JComboBox

Constructor Description
JComboBox() Creates a JComboBox with a default data model.
JComboBox(E[] items) Creates a JComboBox that contains the elements in the specified array.
JComboBox(Vector<E> items) Creates a JComboBox that contains the elements in the specified vector.
JComboBox(ComboBoxModel<E> aModel) Creates a JComboBox that contains the elements in the specified ComboBoxModel.

 

Creating a JComboBox

 

Example -1: Creating a Simple JComboBox on JFrame

package jcombobox;

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.Vector;

public class JComboxBoxExample1 extends JFrame {
	public JComboxBoxExample1() {
		// Set up the frame
		setTitle("JComboBox Example");
		setSize(400, 300);
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setLayout(new FlowLayout());

		// JComboBox with default data model
		JComboBox<String> comboBox1 = new JComboBox<>();
		comboBox1.addItem("India");
		comboBox1.addItem("USA");
		comboBox1.addItem("China");
		comboBox1.addItem("Germany");
		comboBox1.addItem("Canada");
		comboBox1.addItem("Japan");
		comboBox1.setEditable(true);
		
		// Adding JComboBox components to the frame
		add(new JLabel("Select Country:"));
		add(comboBox1);
		
		// Showing the frame
		setVisible(true);
	}

	public static void main(String[] args) {
		new JComboxBoxExample1();
	}
}

 

Output

 

 

Example -2: Creating Multiple JComboBox on JFrame

package jcombobox;

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.Vector;

public class JComboxBoxExample2 extends JFrame {
	public JComboxBoxExample2() {
		// Set up the frame
		setTitle("JComboBox Example");
		setSize(700, 200);
		setLocation(200, 100);
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setLayout(new FlowLayout());

		// JComboBox with default data model
		JComboBox<String> comboBox1 = new JComboBox<>();
		comboBox1.addItem("India");
		comboBox1.addItem("USA");
		comboBox1.addItem("China");
		comboBox1.addItem("Germany");
		comboBox1.addItem("Canada");
		comboBox1.addItem("Japan");
		comboBox1.setEditable(true);

		// Adding JComboBox components to the frame
		add(new JLabel("Select Country:"));
		add(comboBox1);

		// JComboBox with an array of items
		String[] items = {"Apple", "Orange", "Grapes", "Mango", "Guava"};
		JComboBox<String> comboBox2 = new JComboBox<>(items);

		// Adding JComboBox components to the frame
		add(new JLabel("Select Fruit:"));
		add(comboBox2);

		// JComboBox with a Vector of items
		Vector<String> vectorItems = new Vector<>();
		vectorItems.add("Java");
		vectorItems.add("Python");
		vectorItems.add("C++");
		vectorItems.add("PHP");
		JComboBox<String> comboBox3 = new JComboBox<>(vectorItems);

		// Adding JComboBox components to the frame
		add(new JLabel("Select Fruit:"));
		add(comboBox3);

		// JComboBox with a custom ComboBoxModel
		DefaultComboBoxModel<String> model = new DefaultComboBoxModel<>(
				new String[]{"Dog", "Cat", "Rabbit","Bird","Parrots","Horse"});
		JComboBox<String> comboBox4 = new JComboBox<>(model);

		// Additional JComboBox methods demonstration
		comboBox4.addItem("Reptiles");
		comboBox4.removeItem("Cat");
		comboBox4.setSelectedIndex(2);
		comboBox4.setRenderer(new DefaultListCellRenderer());

		// Adding JComboBox components to the frame
		add(new JLabel("Select Fruit:"));
		add(comboBox4);

		// Showing the frame
		setVisible(true);
	}

	public static void main(String[] args) {
		new JComboxBoxExample2();
	}
}

 

Output

 

 

Summary of Common Methods in JComboBox

Method Description
void addItem(E item) Adds an item to the item list.
void removeAllItems() Removes all items from the item list.
void removeItem(Object anObject) Removes the first occurrence of an item from the item list.
void removeItemAt(int anIndex) Removes the item at the specified index.
void setEditable(boolean aFlag) Sets the JComboBox to be editable or not.
boolean isEditable() Returns true if the JComboBox is editable.
void setSelectedItem(Object anObject) Sets the selected item in the JComboBox.
Object getSelectedItem() Returns the currently selected item.
void setSelectedIndex(int anIndex) Sets the selected item to the item at the specified index.
int getSelectedIndex() Returns the index of the currently selected item.
void setEditor(ComboBoxEditor anEditor) Sets the editor used to edit the selected item in the JComboBox.
ComboBoxEditor getEditor() Returns the editor used to edit the selected item in the JComboBox.
void setPopupVisible(boolean v) Sets the visibility of the popup.
boolean isPopupVisible() Returns true if the popup is currently visible.
void showPopup() Causes the popup to be displayed.
void hidePopup() Causes the popup to be hidden.

 

 

 


End of the lesson….enjoy learning

 

 

Student Ratings and Reviews

 

 

 

There are no reviews yet. Be the first one to write one.

 

 

Submit a Review