Java JComboBox - Fill From Array.

May 10, 2017 Oclemy Java JComboBox, Java Array 1 minute, 31 seconds

So yes,we are continuing with Java swing.Today we see how to populate JCombobox from an array :

  • Populate an array with data.
  • Pass the array to our DefaultComboBoxModel constructor.
  • Set the defaultComboBox model to our JComboBox.

 

package jcombobox.array;

//IMPORTS
import javax.swing.DefaultComboBoxModel;

//CLASS
public class JComboArray extends javax.swing.JFrame {

    //CONSTRUCTOR
    public JComboArray() {
        initComponents();
    }

    //BIND DATA
   private void populate()
   {
       String[] spacecrafts={"Casini","Juno","Apollo 15","Kepler","New Horizon","Galileo","Enterprise","James Web","Voyager","Spitzer"};
       DefaultComboBoxModel dm=new DefaultComboBoxModel(spacecrafts);
       jComboBox1.setModel(dm);
   }

    @SuppressWarnings("unchecked")
    //THIS GENERATED BY NETBEANS
    // <editor-fold defaultstate="collapsed" desc="Generated Code">                          
    private void initComponents() {

        jComboBox1 = new javax.swing.JComboBox();
        populateBtn = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jComboBox1.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));

        populateBtn.setText("Populate");
        populateBtn.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                populateBtnActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(181, 181, 181)
                        .addComponent(jComboBox1, javax.swing.GroupLayout.PREFERRED_SIZE, 206, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(236, 236, 236)
                        .addComponent(populateBtn)))
                .addContainerGap(195, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(86, 86, 86)
                .addComponent(jComboBox1, javax.swing.GroupLayout.PREFERRED_SIZE, 29, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(56, 56, 56)
                .addComponent(populateBtn)
                .addContainerGap(154, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>                        

    //POPULATE BTN CLICKED
    private void populateBtnActionPerformed(java.awt.event.ActionEvent evt) {                                            
        populate();

    }                                           

    //MAIN METHODS
    public static void main(String args[]) {

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new JComboArray().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify                     
    private javax.swing.JComboBox jComboBox1;
    private javax.swing.JButton populateBtn;
    // End of variables declaration                   
}

Best Regards.

Comments