JList add/remove Item
Asked Answered
M

2

9

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 remove the selected item from the first JList Here's the code:

First list

private javax.swing.JList listaRosa;

Populated by this method:

private void visualizzaRosaButtonvisualizzaRosa(java.awt.event.ActionEvent evt) {                                                    
    // TODO add your handling code here:
    visualizzaSquadraSelezionata();
    String fileSquadra;
    fileSquadra = squadraDaVisualizzare.getText();
    DefaultListModel listModel = new DefaultListModel();
    try {
        FileInputStream fstream = new FileInputStream("C:/Users/Franky/Documents/NetBeansProjects/JavaApplication5/src/javaapplication5/Rose/"+fileSquadra+"");
        // Get the object of DataInputStream
        DataInputStream in = new DataInputStream(fstream);
        BufferedReader br = new BufferedReader(new InputStreamReader(in));
        String strLine;
        //Read File Line By Line
        while ((strLine = br.readLine()) != null)   {
            listModel.addElement(strLine);
            System.out.println(strLine);
        }
        listaRosa.setModel(listModel);
        //Close the input stream
        in.close();
    } catch (Exception e) {
    }

The second list, where I want to insert items removing from the first:

private javax.swing.JList listaTitolari

Here's the NOT working code:

private void aggiungiTitolareButtonActionPerformed(java.awt.event.ActionEvent evt) {                                                       
    // TODO add your handling code here:
    DefaultListModel listModel = new DefaultListModel();
    String daInserire;
    listModel.addElement(listaRosa.getSelectedValue());
    listModel.removeElement(listaRosa.getSelectedValue());
    listaTitolari.setModel(listModel);
} 

Thanks

Modie answered 6/3, 2011 at 19:57 Comment(0)
S
17

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());        
} 
Swaney answered 6/3, 2011 at 20:2 Comment(4)
still owerwriting the inserted element :(Modie
@Franky Is there any exception?Swaney
@Franky havent got solution. I tried the above code but i was not able remove an item from jlist.Biocellate
as above, you have to initialize defaultlistmodel out of the methodModie
P
11

The best and easiest way to clear a JLIST is:

myJlist.setListData(new String[0]);
Phthisis answered 19/11, 2012 at 12:20 Comment(2)
This will replace the ListModel, which may not be desirable.Harper
@dmoloney per the comment above, they did say "...to clear a JLIST". This solution accomplishes just that.Relativity

© 2022 - 2024 — McMap. All rights reserved.