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

JList Component in Java

tempacc123

User Growth Hacker
T Rep
0
0
0
Rep
0
T Vouches
0
0
0
Vouches
0
Posts
155
Likes
64
Bits
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:
  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. String

    [

    ]

    colors =

    {

    "Blue"

    , "Green"

    , "Yellow"

    , "Orange"

    , "Cyan"

    , "Brown"

    , "Black"

    , "White"

    , "Pink"

    , "Red"

    }

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

    class

    jListComponent{
  4. public

    static

    void

    main(

    String

    [

    ]

    args)

    {
  5. JFrame

    frame =

    new

    JFrame

    (

    "JList Component"

    )

    ;

  6. String

    [

    ]

    colors =

    {

    "Blue"

    , "Green"

    , "Yellow"

    , "Orange"

    , "Cyan"

    , "Brown"

    , "Black"

    , "White"

    , "Pink"

    , "Red"

    }

    ;
  7. JList

    list =

    new

    JList

    (

    colors)

    ;

  8. frame.getContentPane

    (

    )

    .add

    (

    new

    JScrollPane

    (

    list)

    )

    ;
  9. frame.getContentPane

    (

    )

    .setLayout

    (

    new

    FlowLayout

    (

    )

    )

    ;
  10. frame.pack

    (

    )

    ;
  11. frame.setSize

    (

    250

    , 200

    )

    ;
  12. frame.setVisible

    (

    true

    )

    ;
  13. frame.setDefaultCloseOperation

    (

    JFrame

    .EXIT_ON_CLOSE

    )

    ;
  14. }
  15. }

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.
 

452,292

323,341

323,350

Top