开发者

JList selected item to String

开发者 https://www.devze.com 2023-04-02 07:22 出处:网络
Cou开发者_JAVA百科ld someone tell me how to get the selected item of JList to a String? It is a single selection JList.Try this:

Cou开发者_JAVA百科ld someone tell me how to get the selected item of JList to a String? It is a single selection JList.


Try this:

String selected = jList1.getSelectedValue();


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

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);
0

精彩评论

暂无评论...
验证码 换一张
取 消