Free Data Structures and Algorithms Course

Subscribe below and get all best seller courses for free !!!


Subscribe to all free courses

How to use Combo Box (Drop-Down list) in Java using Swing API ?

A Simple program demonstrating the use of Combo Box (Drop-Down list) in Java using Swing.

Click here to download complete source code 

package com.hubberspot.swing.example;

import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;

public class ComboBoxDemo extends JFrame {
  private String[] languages = {
    "Java", "C++", "C", "Python",
    "JavaScript", "Perl", "Ruby", "C#"
  private JTextField textfield1 = new JTextField
   ("Top Programming Languages : ");

  private JTextField textfield2 = new JTextField(15);
  private JComboBox comboBox = new JComboBox();
  private int count = 0;

  public ComboBoxDemo() {
 for(int i = 0; i < languages.length; i++)

 comboBox.addActionListener(new ActionListener() { 
   public void actionPerformed(ActionEvent e) {
  textfield2.setText("You Selected : " +      
 setLayout(new FlowLayout());
  public static void main(String[] args) {
 setFrame(new ComboBoxDemo(), 250, 150);

  public static void 
  setFrame(final JFrame frame, final int width, final int height) {
 SwingUtilities.invokeLater(new Runnable() {
   public void run() {
     frame.setSize(width, height);

Output of the program : 


Video tutorial to demonstrate how to use Combo Box (Drop Down list) in Java using Swing API.

Video tutorial to demonstrate how to create a simple Swing Application in Java.

© 2021 Learn Java by Examples Template by Hubberspot