Jamie
Jamie

Reputation: 525

JList selected item to String

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

Upvotes: 13

Views: 84931

Answers (4)

The answers given above doesn't work or are not so advanced as on today. The following code could be used inside the ListSelectionEvent.

String selected = jList1.getSelectedValue().toString();
jTextArea1.append("Selected item is  " + selected);

Upvotes: 1

Swaranga Sarma
Swaranga Sarma

Reputation: 13423

Try this:

String selected = jList1.getSelectedValue();

Upvotes: 32

Aouidane Med Amine
Aouidane Med Amine

Reputation: 1681

private void jList1MouseClicked(java.awt.event.MouseEvent evt) {

    Object sel =null;

    int[] selectedIx = this.jList1.getSelectedIndices();      

    for (int i = 0; i < selectedIx.length; i++) {
        sel = jList1.getModel().getElementAt(selectedIx[i]);
    }

    System.out.println(sel);
}

Upvotes: 1

user1333035
user1333035

Reputation: 181

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

String a = jList1.getSelectedValue().toString();

Upvotes: 18

Related Questions