Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JList selected item to String

Tags:

java

swing

Could someone tell me how to get the selected item of JList to a String? It is a single selection JList.

like image 325
Jamie Avatar asked Sep 05 '11 14:09

Jamie


People also ask

How do I remove a selected item from a JList in Java?

The JList component is backed by a list model. So the only recommended way to remove an item from the list view is to delete it from the model (and refresh the view). If he's using a DefaultListModel (and in all likelihood he is), then there's no need to refresh the view since this should be done automatically.

How do I select multiple items in JList?

Multiple selection list enables the user to select many items from a JList. A SINGLE_INTERVAL_SELECTION list allows selection of a contiguous range of items in the list by clicking the first item, then holding the Shift key while clicking the last item to select in the range.


2 Answers

Try this:

String selected = jList1.getSelectedValue();
like image 76
Swaranga Sarma Avatar answered Oct 05 '22 23:10

Swaranga Sarma


If you want the value of the selected item to a string you should try

String a = jList1.getSelectedValue().toString();
like image 23
user1333035 Avatar answered Oct 05 '22 22:10

user1333035