Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java - How do I get a all the selected values from a JList?

A struggling Java newbie here - help! I'm trying to: - Get all the selected values from a JList - Create an ArrayList from those values

It seems getSelectedValues is deprecated?

like image 309
Alex G Avatar asked Jan 16 '12 21:01

Alex G


People also ask

How get values from JList?

We can display a value when an item is selected from a JList by implementing MouseListener interface or extending MouseAdapter class and call the getClickCount() method with single-click event (getClickCount() == 1) of MouseEvent class.

How do I iterate through a JList?

This can be done by calling JList 's getModel() method which return a ListModel object. From the ListModel we can get the items size, and we can iterate the entire items of the JList component.

How do I select multiple items in JList?

You can access the selected indexes at any point after the selection event(s) occurs. The method JList. getSelectedIndices returns an array of currently selected indexes, and getSelectedValuesList() returns the actual items depending on what you want....


3 Answers

Until JDK 1.6 (deprecated in 1.7):

  • public Object[] getSelectedValues()

New since JDK 1.7:

  • public List<E> getSelectedValuesList()

    Returns a list of all the selected items, in increasing order based on their indices in the list.

like image 80
miku Avatar answered Oct 01 '22 03:10

miku


As of JDK1.7 it was replaced with public List<E> getSelectedValuesList(). http://docs.oracle.com/javase/7/docs/api/javax/swing/JList.html#getSelectedValuesList%28%29

like image 30
aoi222 Avatar answered Oct 01 '22 04:10

aoi222


use getSelectedValuesList instead.

So, use public List<E> getSelectedValuesList().

It returns:

Returns a list of all the selected items, in increasing order based on their indices in the list.

like image 23
RanRag Avatar answered Oct 01 '22 02:10

RanRag