Java jlist basic tutorial and examples
I can provide you with a basic tutorial and examples for using JList in Java.
JList is a Swing component that provides a list of items to the user. You can use JList to display a list of items, and the user can select one or more items from the list.
Here's an example of creating a simple JList:
import javax.swing.JFrame; import javax.swing.JList; import javax.swing.JPanel; import javax.swing.JScrollPane; public class MyList extends JFrame { public MyList() { super("List Example"); String[] items = {"Item 1", "Item 2", "Item 3", "Item 4", "Item 5"}; JList<String> list = new JList<>(items); JScrollPane scrollPane = new JScrollPane(list); JPanel panel = new JPanel(); panel.add(scrollPane); add(panel); pack(); setLocationRelativeTo(null); setVisible(true); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } public static void main(String[] args) { new MyList(); } }Soucre:www.theitroad.com
In this example, we create a new JList with an array of strings representing the items to display. We then create a new JScrollPane with the JList, add the JScrollPane to a JPanel, and add the JPanel to the JFrame using add()
. We call the pack()
method to resize the JFrame to fit its contents, center it on the screen using setLocationRelativeTo()
, make it visible with setVisible(true)
, and set the default close operation to exit the application when the JFrame is closed.
When you run this program, it will display a window with a list of items.
Here's an example of creating a JList with a custom list model:
import java.util.ArrayList; import java.util.List; import javax.swing.DefaultListModel; import javax.swing.JFrame; import javax.swing.JList; import javax.swing.JPanel; import javax.swing.JScrollPane; public class MyList extends JFrame { public MyList() { super("List Example"); List<String> items = new ArrayList<>(); items.add("Item 1"); items.add("Item 2"); items.add("Item 3"); items.add("Item 4"); items.add("Item 5"); DefaultListModel<String> model = new DefaultListModel<>(); for (String item : items) { model.addElement(item); } JList<String> list = new JList<>(model); JScrollPane scrollPane = new JScrollPane(list); JPanel panel = new JPanel(); panel.add(scrollPane); add(panel); pack(); setLocationRelativeTo(null); setVisible(true); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } public static void main(String[] args) { new MyList(); } }
In this example, we create a new JList with a custom DefaultListModel that we populate with an ArrayList of items. We then add each item to the model using addElement()
and create a new JList with the model. We create a new JScrollPane with the JList, add the JScrollPane to a JPanel, and add the JPanel to the JFrame using add()
. We call the pack()
method to resize the JFrame to fit its contents, center it on the screen using setLocationRelativeTo()
, make it visible with setVisible(true)
, and set the default close operation to exit the application when the JFrame is closed.
When you run this program, it will display a window with a list of items.
I hope this helps you get started with using JList in Java!