Load a String Array to jComboBox in Java

0
42

This tutorial entitled “ Load String Array to jComboBox using Java ” will use a String array and load it to a jComboBox element of JFrame Form. Please follow all the steps to complete this tutorial.

An array is a data structure provided by Java, which stores a fixed-size sequential collection of elements of the same type. An array can be set as String, Integer, Double, and Float in Java Datatypes.

Load String Array to jComboBox using Java Steps

  1. Add a new JFrame Form and name it what you want. In my Form, I named it as “ArrayOfText”.

2. Design your form the same with the image below.

3. Create a new method just look like the code below.

  1. public final void loadUniversities(){
  2.  
  3. }

4. Insert the following code inside your new created method.

  1. String[] unv = {
  2. "UM","USEP","UIC","UP","STI","AMA"
  3. };
  4. jComboBox1.setModel(new javax.swing.DefaultComboBoxModel(unv));

unv, jComboBox1 – variable name

5. Copy the method name and insert it into your form load event. In java and Netbeans, the form load event is in method that contains “initComponents”. Just paste the method name after the “initComponent.

  1. public ArrayOfText() {
  2. initComponents();
  3. loadUniversities();
  4. }

6. Run your program and it should look like the image below.

7. Complete Source Code.

  1. public class ArrayOfText extends javax.swing.JFrame {
  2. public ArrayOfText() {
  3. initComponents();
  4. loadUniversities();
  5. }
  6. public final void loadUniversities(){
  7. String[] unv = {
  8. "UM","USEP","UIC","UP","STI","AMA"
  9. };
  10. jComboBox1.setModel(new javax.swing.DefaultComboBoxModel(unv));
  11. }
  12.  
  13. }

For more information, feel free to leave your comment below, use the contact page of this website or use my contact information.
Email: info@kmjtech.net | Cell. No.: 09468362375

Facebook Comments
(Visited 46 times, 1 visits today)

LEAVE A REPLY

Please enter your comment!
Please enter your name here