JList tips and examples

I've been working with the Java JList a fair amount recently, so I thought I'd include a short collection JList examples here while everything is still fresh.

JList single selection

Many times when working with a JList, you want to limit the user to selecting only one item in the list. To configure a JList to allow only a single selection, use the SINGLE_SELECTION property, like this:

// let the user choose only one item in the jlist
menuList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);

Display a JList in a single column

To display a JList in one column, set the JList layout VERTICAL property, as shown here:

//make a jlist single-column
menuList.setLayoutOrientation(JList.VERTICAL);

More JList tips and examples

I initially had a JList listener example here, but I decided to split that off to a separate article to make it easier to find. That content is now located at this JList ListSelectionListener tutorial link.

Other than that, I'll add more JList examples and tips here as I run across them.