JList Component in Java

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:
  1. import java.awt.*; //used to access the FlowLayout as the layout manager
  2. 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.
  1. JFrame frame = new JFrame("JList Component");
  2.  
  3. String[] colors = { "Blue", "Green", "Yellow", "Orange" , "Cyan", "Brown", "Black", "White", "Pink", "Red"};
  4. 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:
  1. frame.getContentPane().add(new JScrollPane(list));
  2. frame.getContentPane().setLayout(new FlowLayout());
5. Lastly, set the size, visibility, and the close operation of the frame. Have this code below:
  1. frame.setSize(250, 200);
  2. frame.setVisible(true);
  3. frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Output: Here's the full code of this tutorial:
  1. import java.awt.*; //used to access the FlowLayout as the layout manager
  2. import javax.swing.*; //used to access the JFrame, JScrollPane, and JList class
  3.  
  4.  
  5. public class jListComponent{
  6. public static void main(String[] args) {
  7. JFrame frame = new JFrame("JList Component");
  8.  
  9. String[] colors = { "Blue", "Green", "Yellow", "Orange" , "Cyan", "Brown", "Black", "White", "Pink", "Red"};
  10. JList list = new JList(colors);
  11.  
  12. frame.getContentPane().add(new JScrollPane(list));
  13. frame.getContentPane().setLayout(new FlowLayout());
  14. frame.pack();
  15. frame.setSize(250, 200);
  16. frame.setVisible(true);
  17. frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  18. }
  19. }
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