• We just launched and are currently in beta. Join us as we build and grow the community.

JComboBox Component in Java

chrislee0706

Deep Tech Visionary
C Rep
0
0
0
Rep
0
C Vouches
0
0
0
Vouches
0
Posts
130
Likes
102
Bits
2 MONTHS
2 2 MONTHS OF SERVICE
LEVEL 1 200 XP
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:

jcombobox.png

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. public

    class

    jComboBoxComponent extends

    JFrame

    {

  4. String

    days[

    ]

    =

    {

    "Monday"

    , "Tuesday"

    , "Wednesday"

    , "Thursday"

    , "Friday"

    }

    ;

    //string array of days to be added to combobox
  5. JComboBox

    cbo =

    new

    JComboBox

    (

    days)

    ;

    //instantiate a combobox which contains the value of days variable from the string array

  6. public

    jComboBoxComponent(

    )

    {

    //constructor


  7. cbo.addItem

    (

    "Saturday"

    )

    ;

    // add the value of Saturday to the combobox
  8. cbo.addItem

    (

    "Sunday"

    )

    ;

    // add the value of Sunday to the combobox

  9. getContentPane(

    )

    .add

    (

    cbo)

    ;

    //add the combobox to JFrame
  10. getContentPane(

    )

    .setLayout

    (

    new

    FlowLayout

    (

    )

    )

    ;

    // have FlowLayout of JFrame
  11. pack(

    )

    ;

    //pack the frame
  12. setVisible(

    true

    )

    ;

    //set visibility to true
  13. setDefaultCloseOperation(

    JFrame

    .EXIT_ON_CLOSE

    )

    ;

    //exit after clicking the close button
  14. }

  15. public

    static

    void

    main(

    String

    arg[

    ]

    )

    {
  16. new

    jComboBoxComponent(

    )

    ;

    //call the constructor
  17. }
  18. }

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


Download
You must upgrade your account or reply in the thread to view the hidden content.
 

452,496

331,422

331,430

Top