Slide 1
Slide 2
Slide 3

List in java programming (V80)

No Comments

JList

JList is a Swing component that displays a list of items in a vertical list. It allows users to select one or more items from the list. It is commonly used together with a JScrollPane to handle scrolling when the list contains many items.

Example: 


Source Code:

Copied!
    public List() {
        initComponents();
        
        jList1.addListSelectionListener(new javax.swing.event.ListSelectionListener() {
            public void valueChanged(javax.swing.event.ListSelectionEvent evt) {
                if (!evt.getValueIsAdjusting()) { // avoid double events
                    java.util.List selected = jList1.getSelectedValuesList();
                    String text = String.join(", ", selected); // joins items with comma and space
                    lblResult.setText("Selected: " + text);
                }
            }
        });
    }                                                                                  

Result:


Watch the video:


back to top