[java] JList add/remove Item

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

This question is related to java swing add jlist

The answer is


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());        
} 

The best and easiest way to clear a JLIST is:

myJlist.setListData(new String[0]);

Examples related to java

Under what circumstances can I call findViewById with an Options Menu / Action Bar item? How much should a function trust another function How to implement a simple scenario the OO way Two constructors How do I get some variable from another class in Java? this in equals method How to split a string in two and store it in a field How to do perspective fixing? String index out of range: 4 My eclipse won't open, i download the bundle pack it keeps saying error log

Examples related to swing

Calling another method java GUI Read input from a JOptionPane.showInputDialog box Call japplet from jframe Java JTable getting the data of the selected row What does .pack() do? How to add row of data to Jtable from values received from jtextfield and comboboxes How can I check that JButton is pressed? If the isEnable() is not work? Load arrayList data into JTable How to draw a circle with given X and Y coordinates as the middle spot of the circle? Simplest way to set image as JPanel background

Examples related to add

Add a new item to recyclerview programmatically? momentJS date string add 5 days How to add an object to an ArrayList in Java ListView with Add and Delete Buttons in each Row in android How to add new column to MYSQL table? How to add text to an existing div with jquery Datetime in C# add days How to add hours to current time in python How To Add An "a href" Link To A "div"? How to use ArrayList.addAll()?

Examples related to jlist

JList add/remove Item