How to Show an Image when Dropdown Item Changed in Swing

Swing – Show an Image when Dropdown Item changed

In this example, We will show simple Java program, How to show an Image when Dropdown Item changed in Swing. The example program has been tested and shared in the same post.

Project Structure

How to Show an Image when Dropdown Item Changed in Swing

Sample Program

package com.dineshkrish;

import java.awt.BorderLayout;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;

import javax.swing.ImageIcon;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;

 * @author Dinesh Krishnan

public class DropdownExample extends JFrame {

	private JComboBox<String> comboBox;
	private JLabel label;

	public DropdownExample(String title) {

		// setting JFrame properties
		super.setSize(500, 250);
		super.setLayout(new BorderLayout());


	private void init() {

		comboBox = new JComboBox<String>();

		File file = new File("images");

		// setting the items to combo-box
		for (String name : file.list()) {

			comboBox.addItem(name.replaceAll(".png", "").toUpperCase());

		comboBox.addItemListener(new ItemListener() {

			public void itemStateChanged(ItemEvent event) {

				// calling a method when Item changed in the drop-down
				doSomething((String) event.getItem());


		label = new JLabel();
		label.setSize(100, 100);

		this.add(comboBox, BorderLayout.NORTH);
		this.add(label, BorderLayout.SOUTH);

	private void doSomething(String selectedValue) {

		// changing the label icon image
		ImageIcon icon = new ImageIcon("images/" + selectedValue.toLowerCase() + ".png");

	public static void main(String[] args) {

		new DropdownExample("Dropdown Example - Show Image when Item Selected");



How to Show an Image when Dropdown Item Changed in Swing


1. How to Create a Dropdown using Java
2. How to Call a Method when Dropdown Item selected in Swing

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