JList add/remove Item

前端 未结 2 711
一生所求
一生所求 2020-12-20 17:00

Hi I have to pick an element from a JList to another, removing it from the first The method I\'ve created inserts only one element, overwriting the last one and doesn\'t rem

相关标签:
2条回答
  • 2020-12-20 17:07

    The problem is

    listModel.addElement(listaRosa.getSelectedValue());
    listModel.removeElement(listaRosa.getSelectedValue());
    

    you may be adding an element and immediatly removing it since both add and remove operations are on the same listModel.

    Try

    private void aggiungiTitolareButtonActionPerformed(java.awt.event.ActionEvent evt) {                                                       
    
        DefaultListModel lm2 = (DefaultListModel) listaTitolari.getModel();
        DefaultListModel lm1  = (DefaultListModel) listaRosa.getModel();
        if(lm2 == null)
        {
            lm2 = new DefaultListModel();
            listaTitolari.setModel(lm2);
        }
        lm2.addElement(listaTitolari.getSelectedValue());
        lm1.removeElement(listaTitolari.getSelectedValue());        
    } 
    
    0 讨论(0)
  • 2020-12-20 17:11

    The best and easiest way to clear a JLIST is:

    myJlist.setListData(new String[0]);
    
    0 讨论(0)
提交回复
热议问题