How do I create a JList component?

Category: javax.swing, viewed: 1784 time(s).

JList is a component that displays a list of objects and allow user to select one or more items. To create an instance of JList we can pass a vector, an array of objects or a ListModel. In this example we'll passing an array of objects that contains a date, string and numbers as the parameters.

By default the list does not display a scrollbar. To give our JList component a scrollbar we must wrap it with a JScrollPane.

package org.kodejava.example.swing;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.util.Date;

public class CreateJListDemo extends JFrame {

    public CreateJListDemo() {
        initialize();
    }

    //
    // Initialize the components and configuration of our CreateJListDemo.
    //
    private void initialize() {
        //
        // Define the window title, size and the default close operation.
        //
        this.setTitle("Create JList Demo");
        this.setSize(300, 175);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        //
        // Create an array of arbitrary objects for the JList to display.
        //
        Object[] data = new Object[] {
                new Date(), "One", 1, Long.valueOf("12345"), "Four", "Five"
        };

        //
        // Create an instance of JList and pass data variable as the
        // initial content of it. By default the JList does not have a
        // scrolling behaviour, so we create a JScrollPane as the container
        // for the JList.
        //
        JList list = new JList(data);
        JScrollPane scrollPane = new JScrollPane(list);

        //
        // Add a button to close the program.
        //
        JButton button = new JButton("Close");
        button.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                System.exit(0);
            }
        });

        //
        // Set the panel layout to BorderLayout and place the list on the
        // center and the button on the south.
        //
        this.setLayout(new BorderLayout(5, 5));
        getContentPane().add(scrollPane, BorderLayout.CENTER);
        getContentPane().add(button, BorderLayout.SOUTH);
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                new CreateJListDemo().setVisible(true);
            }
        });
    }

}
Click here to lend your support to: Kode Java Org and make a donation at www.pledgie.com !

 

Can't find what you are looking for? Join our FORUMS and ask some questions!
Download Hundreds of Complimentary Industry Resources

Get hundreds of popular Industry magazines, white papers, webinars, podcasts, and more; all available at no cost to you. With more than 600 complimentary offers, you'll find plenty of titles to suit your professional interests and needs. Click Here and Sign up today!

Sponsored Links

Our Friends

Statistics

Locations of visitors to this page
visitor stats