JList Component in Java
Submitted by donbermoy on Friday, November 21, 2014 - 09:08.
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.
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
- import java.awt.*; //used to access the FlowLayout as the layout manager
- import javax.swing.*; //used to access the JFrame, JScrollPane, and JList class
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.setSize(250, 200);
- frame.setVisible(true);
- 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{
- String[] colors = { "Blue", "Green", "Yellow", "Orange" , "Cyan", "Brown", "Black", "White", "Pink", "Red"};
- frame.pack();
- frame.setSize(250, 200);
- frame.setVisible(true);
- }
- }
Add new comment
- 132 views