tempacc123
User Growth Hacker
2
MONTHS
2 2 MONTHS OF SERVICE
LEVEL 1
400 XP
This is a tutorial in which we will going to create a program that will have a JList Component in Java. A JList is a component that displays a list of objects and lets the user select one or more items that is displayed in one or more columns, to choose from.
So, now let's start this tutorial!
1. Open JCreator or NetBeans and make a java program with a file name of jListComponent.java.
2. Import the following packages:
3. Initialize your variable in your Main, variable frame for JFrame, variable colors for an array string, and variable list for JList.
As you can see the code above, I declare a String array with variable colors. This array contains the names of colors. Remember that an array starts at 0. Then we have created the JList component and instantiated to have the colors value inside the JList.
4. Now, we will have the JScrollPane to attach the JList on it to have a scrollbar. We will use the getContentPane and add method of the frame to attach the scroll pane on the frame. We will also use the flowlayout as our layout manager. Have this code below:
5. Lastly, set the size, visibility, and the close operation of the frame. Have this code below:
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 jListComponent.java.
2. Import the following packages:
- import
java.awt.*
;
//used to access the FlowLayout as the layout manager
- import
javax.swing.*
;
//used to access the JFrame, JScrollPane, and JList class
3. Initialize your variable in your Main, variable frame for JFrame, variable colors for an array string, and variable list for JList.
- JFrame
frame =
new
JFrame
(
"JList Component"
)
;
- String
[
]
colors =
{
"Blue"
, "Green"
, "Yellow"
, "Orange"
, "Cyan"
, "Brown"
, "Black"
, "White"
, "Pink"
, "Red"
}
;
- JList
list =
new
JList
(
colors)
;
As you can see the code above, I declare a String array with variable colors. This array contains the names of colors. Remember that an array starts at 0. Then we have created the JList component and instantiated to have the colors value inside the JList.
4. Now, we will have the JScrollPane to attach the JList on it to have a scrollbar. We will use the getContentPane and add method of the frame to attach the scroll pane on the frame. We will also use the flowlayout as our layout manager. Have this code below:
- frame.getContentPane
(
)
.add
(
new
JScrollPane
(
list)
)
;
- frame.getContentPane
(
)
.setLayout
(
new
FlowLayout
(
)
)
;
5. Lastly, set the size, visibility, and the close operation of the frame. Have this code below:
- frame.setSize
(
250
, 200
)
;
- frame.setVisible
(
true
)
;
- frame.setDefaultCloseOperation
(
JFrame
.EXIT_ON_CLOSE
)
;
Output:
Here's the full code of this tutorial:
- import
java.awt.*
;
//used to access the FlowLayout as the layout manager
- import
javax.swing.*
;
//used to access the JFrame, JScrollPane, and JList class
- public
class
jListComponent{
- public
static
void
main(
String
[
]
args)
{
- JFrame
frame =
new
JFrame
(
"JList Component"
)
;
- String
[
]
colors =
{
"Blue"
, "Green"
, "Yellow"
, "Orange"
, "Cyan"
, "Brown"
, "Black"
, "White"
, "Pink"
, "Red"
}
;
- JList
list =
new
JList
(
colors)
;
- frame.getContentPane
(
)
.add
(
new
JScrollPane
(
list)
)
;
- frame.getContentPane
(
)
.setLayout
(
new
FlowLayout
(
)
)
;
- frame.pack
(
)
;
- frame.setSize
(
250
, 200
)
;
- frame.setVisible
(
true
)
;
- frame.setDefaultCloseOperation
(
JFrame
.EXIT_ON_CLOSE
)
;
- }
- }
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 hidden text.