How to Create a Dropdown using Java

Swing – Create a Dropdown using Java

In this example, We will show you simple program to Create a Dropdown using Java. The example program snippet has been tested and shared with the output.

Sample Program

package com.dineshkrish;

import java.awt.BorderLayout;

import javax.swing.JComboBox;
import javax.swing.JFrame;

/**
 * 
 * @author Dinesh Krishnan
 *
 */

public class DropdownExample extends JFrame {

	private JComboBox<String> comboBox;

	public DropdownExample(String title) {

		// setting JFrame properties
		super.setTitle(title);
		super.setDefaultCloseOperation(EXIT_ON_CLOSE);
		super.setSize(500, 100);
		super.setVisible(true);
		super.setLayout(new BorderLayout());

		init();
	}

	private void init() {

		comboBox = new JComboBox<String>();

		// setting the items to combo-box
		comboBox.addItem("One");
		comboBox.addItem("Two");
		comboBox.addItem("Three");
		comboBox.addItem("Four");
		comboBox.addItem("Five");

		this.add(comboBox, BorderLayout.NORTH);
	}

	public static void main(String[] args) {

		new DropdownExample("Dropdown Example");
	}

}

Output

How to Create a Dropdown using Java

Hello, folks, I am a founder of dineshkrish.com. I love Java and Open source technologies, If you find my tutorials are useful, please consider making donations to these charities.