List

We can create single or multiple selection lists.

To create a List use:

List(int numberOfVisibleItems, boolean allowMultipleSelection)  

To add an item to the end of a list use:

void add(String s)  

To overwrite an item into a list use:

replaceItem(String newValue, int index) 

To mark an item as being selected in a list use:

select(int index) 

To check if an item is selected use:

boolean isIndexSelected(int index)  

To remove an item from a list use:

remove(int index)
remove(String s) // first occurrence of s is removed 

To get the selected index use:

int getSelectedIndex() 

To get the selected text use:

String getSelectedItem()    // for single selection list
 String[] getSelectedItem () < style='color:fuchsia'>// for multiple selection list  

To read text from a given index of s use:

String getItem(int index) 

Single Selection List Example: (Run Applet)

import javax.swing.*;
import javax.swing.event.*;

public class SingleSelectionListDemo extends JApplet
{
    @Override
    public void init()
    {
        this.setContentPane(new View());
    }

    public class View extends JPanel implements ListSelectionListener
    {
        private final String names[] =
        {
            "anne", "bill", "colm", "deirdre"
        };
        // JLists can hold any object
        private final JList nameList = new JList<>(names);

        public View()
        {
            super();
            // make list single selection
            this.nameList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);

            this.add(this.nameList);
            this.nameList.addListSelectionListener(this);
        }

        @Override
        public void valueChanged(ListSelectionEvent e)
        {
            final String selectedValue = (String)(this.nameList.getSelectedValue());

            showStatus("Name selected was " + selectedValue);
        }
    }
}

Multiple Selection List Example: (Run Applet)

import java.util.List;
import javax.swing.*;
import javax.swing.event.*;

public class MultipleSelectionListDemo extends JApplet
{
    @Override
    public void init()
    {
        this.setContentPane(new View());
    }

    public class View extends JPanel implements ListSelectionListener
    {
        private final String names[] =
        {
            "anne", "bill", "colm", "deirdre"
        };
        private final JList nameList = new JList<>(names);

        public View()
        {
            super();
            // make list multiple selection
            this.nameList.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);

            this.add(this.nameList);
            this.nameList.addListSelectionListener(this);
        }

        @Override
        public void valueChanged(ListSelectionEvent e)
        {
            final List selectedValues = this.nameList.getSelectedValuesList();
            String selectedItems = "Selected names: ";

            for (int i = 0; i < selectedValues.size(); i++)
            {
                selectedItems += selectedValues.get(i) + " ";
            }
            showStatus(selectedItems);
        }
    }
}

ComboBox

A Choice (drop down list) provides a list from which one item can be selected.

To create a ComboBox use:

Choice()  

To add an item to the end of a ComboBox use:

add(String s)  

To insert an item into a ComboBox use:

String insert(String s, index i) 

To remove an item from a ComboBox use:

remove(int index)
remove(String s) //first occurrence of s is removed 

To get the selected index use:

int getSelectedIndex() 

To get the selected text use:

String getSelectedItem() 

To read text from a given index of ComboBox use:

String getItem (int index) 

ComboBox Example: (Run Applet)

import java.awt.event.*;
import javax.swing.*;

public class ComboBoxDemo extends JApplet
{
    @Override
    public void init()
    {
        this.setContentPane(new View());
    }

    public class View extends JPanel implements ItemListener
    {
        private JComboBox list = new JComboBox();

        public View()
        {
            super();

            this.list.addItem("Bill");
            this.list.addItem("Mary");
            this.list.addItem("Joe");
            this.list.addItem("Anne");

            this.add(this.list);
            this.list.addItemListener(this);
        }

        @Override
        public void itemStateChanged(ItemEvent e)
        {
            showStatus("Name selected was: " + this.list.getSelectedItem());
        }
    }
}
 
<div align="center"><a href="../versionC/index.html" title="DKIT Lecture notes homepage for Derek O&#39; Reilly, Dundalk Institute of Technology (DKIT), Dundalk, County Louth, Ireland. Copyright Derek O&#39; Reilly, DKIT." target="_parent" style='font-size:0;color:white;background-color:white'>&nbsp;</a></div>