JList selected item to String

后端 未结 4 2236
清酒与你
清酒与你 2021-02-19 15:10

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

相关标签:
4条回答
  • 2021-02-19 15:29

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

    String a = jList1.getSelectedValue().toString();
    
    0 讨论(0)
  • 2021-02-19 15:38

    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 讨论(0)
  • 2021-02-19 15:43
    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);
    }
    
    0 讨论(0)
  • 2021-02-19 15:52

    Try this:

    String selected = jList1.getSelectedValue();
    
    0 讨论(0)
提交回复
热议问题