Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 940
Fseee Avatar asked Mar 06 '11 19:03

Fseee


People also ask

How add and remove from JList?

However, JList has no method to add or delete items once it is initialized. Instead, if you need to do this, you must use the ListModel class with a JList. Think of the ListModel as the object which holds the items and the JList as the displayer.

How do I remove an item from JList?

To actually remove the item, we use the syntax . remove(int); where the integer is the index of the item you wish to remove. That is how we add and remove things from a JList, and this concludes our tutorial on the JList.

How do I add a button to a JList?

Effectively, you add a MouseListener to your JList , determine which particular button is being rendered at that click-point, then programmatically click that button. Or, you could make a JPanel of buttons, and place the panel in a JScrollPane .

How do I select multiple items in JList?

You can access the selected indexes at any point after the selection event(s) occurs. The method JList. getSelectedIndices returns an array of currently selected indexes, and getSelectedValuesList() returns the actual items depending on what you want....


1 Answers

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());        
} 
like image 100
Bala R Avatar answered Nov 06 '22 10:11

Bala R