Getting the Items in a JList Component : List « Swing JFC « Java






Getting the Items in a JList Component

 
import javax.swing.JList;

public class Main {
  public static void main(String[] argv) throws Exception {
    String[] items = { "A", "B", "C", "D" };
    JList list = new JList(items);

    // Get number of items in the list
    int size = list.getModel().getSize(); // 4

    // Get all item objects
    for (int i = 0; i < size; i++) {
      Object item = list.getModel().getElementAt(i);
    }
  }
}

   
  








Related examples in the same category

1.Use JList component to display custom objects with ListCellRendererUse JList component to display custom objects with ListCellRenderer
2.An example of JList with a DefaultListModelAn example of JList with a DefaultListModel
3.Create JList from array of string valueCreate JList from array of string value
4.A graphical list selection monitorA graphical list selection monitor
5.Test of the DragGesture classes and JList to see if we can recognize a simple drag gestureTest of the DragGesture classes and JList to see if we can recognize a simple drag gesture
6.Dual JList with buttons in betweenDual JList with buttons in between
7.extends ListCellRenderer to display iconsextends ListCellRenderer to display icons
8.Add JList to Scroll paneAdd JList to Scroll pane
9.Dual Sample: JList and ComboBoxDual Sample: JList and ComboBox
10.JList selection changed listenerJList selection changed listener
11.Triple List from same data arrayTriple List from same data array
12.List with and without ScrollPane List with and without ScrollPane
13.Set visible row count and fixed cell height and widthSet visible row count and fixed cell height and width
14.List: Shared Data SampleList: Shared Data Sample
15.List Data Event DemoList Data Event Demo
16.How to use the list componentHow to use the list component
17.Create list from list modelCreate list from list model
18.How to create list cell rendererHow to create list cell renderer
19.React to list selection actionReact to list selection action
20.Construct the list componentConstruct the list component
21.Tab list rendererTab list renderer
22.List with textfield inputList with textfield input
23.Sharing a Model between a JList and JComboBoxSharing a Model between a JList and JComboBox
24.Demonstrate Swing ScrollingListDemonstrate Swing ScrollingList
25.Demonstrate Swing JList ListModelDemonstrate Swing JList ListModel
26.Weak ListModel
27.ListModel DemoListModel Demo
28.ModifyModelSample: ListModel DemoModifyModelSample: ListModel Demo
29.ArrayList with a ListModel for ease of use
30.Drag and Drop:JList and ListDrag and Drop:JList and List
31.List selection event
32.JList is a component that displays a list of objects: It allows the user to select one or more items.JList is a component that displays a list of objects: It allows the user to select one or more items.
33.A JTextArea is a multi-line text area that displays plain text.
34.JTextPane component is an advanced component for working with text.
35.Model for a JButton: manage only the state of the button
36.A default button model
37.JList: ListModel and ListSelectionModel. The ListModel handles data. ListSelectionModel works with the GUI.JList: ListModel and ListSelectionModel. The ListModel handles data. ListSelectionModel works with the GUI.
38.A single-selection JList.
39.Listening for Changes to the Items in a JList Component
40.Listening for Changes to the Selection in a JList Component
41.Detecting Double and Triple Clicks on an Item in a JList Component
42.Arranging Items in a JList Component
43.changes the layout orientation so that its items are displayed top-to-bottom and left-to-right.
44.Make the number of visible rows dependent on the height of the list, the visibleRowCount property must be set to 0:
45.Setting the Selection Mode of a JList Component
46.The selected items must be in a contiguous range
47.Multiple ranges of selected items are allowed
48.Setting the Selected Items in a JList Component
49.import javax.swing.JList;
50.Select all the items
51.Clear all selections
52.Select the first item
53.Add another selection - the third item
54.Deselect the first item
55.Select a single item
56.Getting the Selected Items in a JList Component
57.Get the index of the last selected item
58.Determine if the third item is selected
59.Determine if there are any selected items
60.Return the selected item objects:
61.Adding and Removing an Item in a JList Component
62.Append an item
63.Insert an item at the beginning
64.Create a list that allows adds and removes
65.Set method replaces an item
66.Methods are used to remove items
67.Methods are used to find an item:
68.These methods can be used to find the range of visible items:
69.Get index of first visible item
70.Get index of last visible item
71.Setting a Tool Tip for an Item in a JList Component
72.Setting the Dimensions of an Item in a JList Component
73.It is also possible to set the item dimensions using a sample value:
74.Creating a JList Component
75.The items can be arbitrary objects. The toString() method of the objects is displayed in the list component.
76.Determining the Selected JRadioButton in a Button Group
77.A spinner that rolls from the end of a list to beginning
78.This program demonstrates the use of cell renderers in a list box
79.This program demonstrates a simple fixed list of stringsThis program demonstrates a simple fixed list of strings
80.This program demonstrates a list that dynamically computes list entriesThis program demonstrates a list that dynamically computes list entries