chrislee0706
Deep Tech Visionary
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:
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:
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:
Add the JComboBox to the frame using the add method and have it with the flowlayout layout of the frame.
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.
Output:
Here's the full code of this tutorial:
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
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:
- import
java.awt.*
;
//used to access the flowlayout
- 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:
- String
days[
]
=
{
"Monday"
, "Tuesday"
, "Wednesday"
, "Thursday"
, "Friday"
}
;
//string array of days to be added to combobox
- 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:
- cbo.addItem
(
"Saturday"
)
;
// add the value of Saturday to the combobox
- 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.
- getContentPane(
)
.add
(
cbo)
;
//add the combobox to JFrame
- 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.
- pack(
)
;
//pack the frame
- setVisible(
true
)
;
//set visibility to true
- setDefaultCloseOperation(
JFrame
.EXIT_ON_CLOSE
)
;
//exit after clicking the close button
Output:

Here's the full code of this tutorial:
- import
java.awt.*
;
//used to access the flowlayout
- import
javax.swing.*
;
//used to access the JFrame and JComboBox class
- public
class
jComboBoxComponent extends
JFrame
{
- String
days[
]
=
{
"Monday"
, "Tuesday"
, "Wednesday"
, "Thursday"
, "Friday"
}
;
//string array of days to be added to combobox
- JComboBox
cbo =
new
JComboBox
(
days)
;
//instantiate a combobox which contains the value of days variable from the string array
- public
jComboBoxComponent(
)
{
//constructor
- cbo.addItem
(
"Saturday"
)
;
// add the value of Saturday to the combobox
- cbo.addItem
(
"Sunday"
)
;
// add the value of Sunday to the combobox
- getContentPane(
)
.add
(
cbo)
;
//add the combobox to JFrame
- getContentPane(
)
.setLayout
(
new
FlowLayout
(
)
)
;
// have FlowLayout of JFrame
- pack(
)
;
//pack the frame
- setVisible(
true
)
;
//set visibility to true
- setDefaultCloseOperation(
JFrame
.EXIT_ON_CLOSE
)
;
//exit after clicking the close button
- }
- public
static
void
main(
String
arg[
]
)
{
- new
jComboBoxComponent(
)
;
//call the constructor
- }
- }
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.