[Java Swing] JComboBox in Java – JComboBox in Java

Content
How to create a JComboBox
Getting events for JComboBox

JComboBox is an object that allows you to select an element to click on the arrow of it. For example, we have a JFrame JComboBox selected from the following flags:

JComboBox in Java

Now we are going to learn by creating a JFrame JComboBox like on.

How to create a JComboBox

To create a JComboBox we use the following constructor:
– JComboBox(): Create JComboBox no data
– JComboBox(ComboBoxModel aModel): Create JComboBox with the designated Model
– JComboBox(And[] items): Create JComboBox with the elements in the array E
– JComboBox(Vector items): Create JComboBox with the elements in the vector E

The following will create JFrame like the first post. It contains a main panel JPanel. main panel is set BorderLayout and contain water in JComboBox selected PAGE_START, JLabel displaying the flag of the country are located in select CENTER. In the code used 2 arrFlag String array for application-sign with the name of the country and arrCountry to display the name of the country in JComboBox.

package nguyenvanquan7826.JComboBox;

import java.awt.BorderLayout;

import javax.swing.ImageIcon;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;

public class MyJComboBox extends JFrame {
	private JComboBox<String> cbbCountry;
	private JLabel lbShow;
	private String[] arrFlag = { "vn", "china", "us", "lao", "en" };
	private String[] arrCountry = { "Việt Nam", "Trung Quốc", "Mỹ", "Lào",
			"Anh" };

	public MyJComboBox() {
		// add main panel to JFrame
		add(createMainPanel());
		// set Display
		setTitle("JComboBox");
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setSize(300, 300);
		setLocationRelativeTo(null);
		setVisible(true);
	}

	// create main panel with BorderLayout
	private JPanel createMainPanel() {
		JPanel panel = new JPanel(new BorderLayout());
		panel.setBorder(new EmptyBorder(10, 10, 10, 10));
		// add a JComboBox to top of main panel
		panel.add(cbbCountry = createComboBox(arrCountry),
				BorderLayout.PAGE_START);
		// get flag name with country is selected in JComboBox
		String flag = arrFlag[cbbCountry.getSelectedIndex()];
		// add JLabel to show flag
		panel.add(lbShow = createLabel(flag), BorderLayout.CENTER);
		return panel;
	}

	// create a JComboBox with list item in ArrayList
	private JComboBox<String> createComboBox(String[] arrItem) {
		JComboBox<String> cbb = new JComboBox<String>(arrItem);
		return cbb;
	}

	// create JLabel to show flag with
	private JLabel createLabel(String flagName) {
		JLabel lb = new JLabel(getFlag(flagName));
		return lb;
	}

	// get flag with name is flagName
	private ImageIcon getFlag(String flagName) {
		ImageIcon flag = new ImageIcon(getClass().getResource(
				"/nguyenvanquan7826/JComboBox/" + flagName + ".png"));
		return flag;
	}

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

Default initialization JComboBox, if there are elements in it, the first element will be selected. To determine the element is selected, you can use the method getSelectedIndex() (take place element is selected) or getSelectedItem() (get selected element) depending on how and different purposes. Have you noticed when taking pictures, then needed to be corrected in accordance with the path where your photos, his image was put together package with java file *. (download pictures here)

JComboBox in Java

Have you noticed that in the above code not catch the event when selecting the elements in the JComboBox, when choosing other countries flag unchanged.

Getting events for JComboBox

To be able to catch the event when selecting the elements in the JComboBox we need addActionListener for JComboBox. Then we have implemets Interface ActionListener and overriding methods actionPerformed(ActionEvent and). Then we will compare this method to identify and treat the effects JComboBox. We have completed the following code:

package nguyenvanquan7826.JComboBox;

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.ImageIcon;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;

public class MyJComboBox extends JFrame implements ActionListener {
	private JComboBox<String> cbbCountry;
	private JLabel lbShow;
	private String[] arrFlag = { "vn", "china", "us", "lao", "en" };
	private String[] arrCountry = { "Việt Nam", "Trung Quốc", "Mỹ", "Lào",
			"Anh" };

	public MyJComboBox() {
		// add main panel to JFrame
		add(createMainPanel());
		// set Display
		setTitle("JComboBox");
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setSize(300, 300);
		setLocationRelativeTo(null);
		setVisible(true);
	}

	// create main panel with BorderLayout
	private JPanel createMainPanel() {
		JPanel panel = new JPanel(new BorderLayout());
		panel.setBorder(new EmptyBorder(10, 10, 10, 10));
		// add a JComboBox to top of main panel
		panel.add(cbbCountry = createComboBox(arrCountry),
				BorderLayout.PAGE_START);
		// get flag name with country is selected in JComboBox
		String flag = arrFlag[cbbCountry.getSelectedIndex()];
		// add JLabel to show flag
		panel.add(lbShow = createLabel(flag), BorderLayout.CENTER);
		return panel;
	}

	// create a JComboBox with list item in ArrayList
	private JComboBox<String> createComboBox(String[] arrItem) {
		JComboBox<String> cbb = new JComboBox<String>(arrItem);
		cbb.addActionListener(this);
		return cbb;
	}

	// create JLabel to show flag with
	private JLabel createLabel(String flagName) {
		JLabel lb = new JLabel(getFlag(flagName));
		return lb;
	}

	// get flag with name is flagName
	private ImageIcon getFlag(String flagName) {
		ImageIcon flag = new ImageIcon(getClass().getResource(
				"/nguyenvanquan7826/JComboBox/" + flagName + ".png"));
		return flag;
	}

	// change flag when select items in JComboBox
	private void changeFlag() {
		lbShow.setIcon(getFlag(arrFlag[cbbCountry.getSelectedIndex()]));
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		if (e.getSource() == cbbCountry) {
			changeFlag();
		}
	}

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

Read more: class JComboBox, use JComboBox