Java Swing dynamic Combobox


 

Java Swing dynamic Combobox

In this section, you will learn how to display the data in one combobox related to another combobox.

In this section, you will learn how to display the data in one combobox related to another combobox.

Java Swing dynamic Combobox

In this section, you will learn how to display the data in one combobox related to another combobox. For this, we have created two combo boxes. In the first combobox, we have added four items. When the user selects any item from the first combox box, the related data will get appeared in second combobox.

Here is the code:

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

public class Compare extends JFrame implements ItemListener {
	JComboBox combo, lcombo;

	public Compare() {
		setLayout(null);
		String a[] = { "Select", "Watches", "Mobiles", "shoes" };
		combo = new JComboBox(a);
		combo.setBounds(50, 50, 100, 20);
		combo.addItemListener(this);
		add(combo);

		lcombo = new JComboBox();
		lcombo.addItemListener(this);
		lcombo.setEnabled(false);
		add(lcombo);
		lcombo.setBounds(50, 100, 100, 20);

		setSize(300, 300);
	}

	public void itemStateChanged(ItemEvent e) {
		String b[] = { "Titan", "HMT" };
		String c[] = { "Nokia", "Sony", "Motorola", "Samsung" };
		String d[] = { "Liberty", "Action", "Bata", "Campus", "Relaxo" };

		if (e.getSource() == combo) {
			if (combo.getSelectedItem().equals("Select")) {
				lcombo.setEnabled(false);
			} else if (combo.getSelectedItem().equals("Watches")) {
				lcombo.setEnabled(true);
				lcombo.removeAllItems();
				for (int i = 0; i < b.length; i++) {
					lcombo.addItem(b[i]);
				}
			} else if (combo.getSelectedItem().equals("Mobiles")) {
				lcombo.setEnabled(true);
				lcombo.removeAllItems();
				for (int i = 0; i < c.length; i++) {
					lcombo.removeItem(c[i]);
					lcombo.addItem(c[i]);
				}
			} else if (combo.getSelectedItem().equals("shoes")) {
				lcombo.setEnabled(true);
				lcombo.removeAllItems();
				for (int i = 0; i < d.length; i++) {
					lcombo.addItem(d[i]);
				}
			}
		}
	}

	public static void main(String args[]) {
		(new Compare()).setVisible(true);
	}
}

Output:

On selecting the item from one combobox, related data will get displayed in another one.

Ads