Could someone tell me how to get the selected item of JList
to a String
? It is a single selection JList
.
试试这个:
String selected = jList1.getSelectedValue();
如果您希望所选项目的值为字符串,则应尝试
String a = jList1.getSelectedValue().toString();
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);
}
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);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.