-Java JComboBox is an object that allows you to select an element by clicking on its arrow. For example, we have a JFrame that selects the flag from the java JComboBox as follows:
-Now we will go find out java JComboBox by creating a JFrame like above.
- JComboBox (ComboBoxModel aModel): Create a JComboBox with the specified model.
- JComboBox (E [] items): Create a JComboBox with elements in the E array
- JComboBox (Vector items): Create JComboBox with elements in Vector E
-The following program will create the same JFrame as the top. It contains the main panel as a JPanel. This main panel is set to BorderLayout and contains a JComboBox that selects country name at PAGE_START, JLabel displays the selected state flag at CENTER. In code, use the two array String arrFlag to get the flag name for the country and arrCountry to display the country name in the java JComboBox.
-File name: MyJComboBox.java
-Continue MyJComboBox.java
-By default, when initializing a java JComboBox, if there are elements in it, the first element is selected. To determine the selected element, we can use the getSelectedIndex () method or the getSelectedItem () method, depending on the method and purpose. Note that when you take the image, you need to correct the path to the location of your image. (download photos here).
-You notice that the code above has not captured events when selecting elements in java JComboBox so when choosing other countries the flag has not changed.
-File name: MyJComboBox.java
-Continue MyJComboBox.java
-The output:
-Read more JPanel GridLayout java examples:
-Now we will go find out java JComboBox by creating a JFrame like above.
How to create a java JComboBox
To create a java JComboBox we use the following constructors:
- JComboBox (): Create JComboBox without data- JComboBox (ComboBoxModel aModel): Create a JComboBox with the specified model.
- JComboBox (E [] items): Create a JComboBox with elements in the E array
- JComboBox (Vector items): Create JComboBox with elements in Vector E
-The following program will create the same JFrame as the top. It contains the main panel as a JPanel. This main panel is set to BorderLayout and contains a JComboBox that selects country name at PAGE_START, JLabel displays the selected state flag at CENTER. In code, use the two array String arrFlag to get the flag name for the country and arrCountry to display the country name in the java JComboBox.
-File name: MyJComboBox.java
-Continue MyJComboBox.java
-By default, when initializing a java JComboBox, if there are elements in it, the first element is selected. To determine the selected element, we can use the getSelectedIndex () method or the getSelectedItem () method, depending on the method and purpose. Note that when you take the image, you need to correct the path to the location of your image. (download photos here).
-You notice that the code above has not captured events when selecting elements in java JComboBox so when choosing other countries the flag has not changed.
Getting events for java JComboBox
-To be able to capture events when selecting elements in java JComboBox we need addActionListener for JComboBox. We then have to implemets the ActionListener interface and override the actionPerformed (ActionEvent e) method. Then we will compare in this method to determine and process the JComboBox is affected. We have the complete code as follows:-File name: MyJComboBox.java
-Continue MyJComboBox.java
-The output:
-Read more JPanel GridLayout java examples:
Comments
Post a Comment