JComboBox Component in Java

This is a tutorial in which we will going to create a program that has the JComboBox component/control using Java. So, now let's start this tutorial! 1. Open JCreator or NetBeans and make a java program with a file name of jComboBoxComponent.java. 2. Import the following packages:
  1. import java.awt.*; //used to access the flowlayout
  2. import javax.swing.*; //used to access the JFrame and JComboBox class
3. The jComboBoxComponent classname must extends the JFrame to have the container of the components used. Have also the following variables for the JComboBox below:
  1.          String days[] = { "Monday", "Tuesday", "Wednesday", "Thursday", "Friday"}; //string array of days to be added to combobox
  2.          JComboBox cbo = new JComboBox(days); //instantiate a combobox which contains the value of days variable from the string array
We have created a String array of days to be added to the combobox. 4. Now, create a constructor to add the JComboBox to the frame as well as its layout and visibility. Another way of adding items to the ComboBox is to have the addItem method. As you can see the String array of days variable above has only Monday to Friday. To add another item, have this code below:
  1.     cbo.addItem("Saturday"); // add the value of Saturday to the combobox
  2.     cbo.addItem("Sunday"); // add the value of Sunday to the combobox
Add the JComboBox to the frame using the add method and have it with the flowlayout layout of the frame.
  1.     getContentPane().add(cbo); //add the combobox to JFrame
  2.     getContentPane().setLayout(new FlowLayout()); // have FlowLayout of JFrame
Then pack the frame so that the component/control will be seen directly to the frame. Also set its visibility to true and have it the close operation to exit.
  1.     pack(); //pack the frame
  2.     setVisible(true); //set visibility to true
  3.     setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //exit after clicking the close button
Output: combobox Here's the full code of this tutorial:
  1. import java.awt.*; //used to access the flowlayout
  2. import javax.swing.*; //used to access the JFrame and JComboBox class
  3.  
  4. public class jComboBoxComponent extends JFrame {
  5.        
  6.          String days[] = { "Monday", "Tuesday", "Wednesday", "Thursday", "Friday"}; //string array of days to be added to combobox
  7.          JComboBox cbo = new JComboBox(days); //instantiate a combobox which contains the value of days variable from the string array
  8.  
  9.      public jComboBoxComponent() { //constructor
  10.    
  11.  
  12.     cbo.addItem("Saturday"); // add the value of Saturday to the combobox
  13.     cbo.addItem("Sunday"); // add the value of Sunday to the combobox
  14.  
  15.     getContentPane().add(cbo); //add the combobox to JFrame
  16.     getContentPane().setLayout(new FlowLayout()); // have FlowLayout of JFrame
  17.     pack(); //pack the frame
  18.     setVisible(true); //set visibility to true
  19.     setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //exit after clicking the close button
  20.   }
  21.  
  22.   public static void main(String arg[]) {
  23.     new jComboBoxComponent(); //call the constructor
  24.   }
  25. }
For more inquiries and need programmer for your thesis systems in any kind of programming languages, just contact my number below. Best Regards, Engr. Lyndon Bermoy IT Instructor/System Developer/Android Developer/Freelance Programmer Mobile: 09488225971 Landline: 826-9296 E-mail:[email protected] Add and Follow me on Facebook: https://www.facebook.com/donzzsky Visit and like my page on Facebook at: https://www.facebook.com/BermzISware

Add new comment