简体   繁体   English

JList添加/删除项目

[英]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: 嗨我必须从JList中选择一个元素到另一个,从第一个中删除它我创建的方法只插入一个元素,覆盖最后一个元素并且不从第一个JList中删除所选项目这里是代码:

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: 这是NOT工作代码:

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 谢谢

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. 您可能正在添加一个元素并立即删除它,因为添加和删除操作都在同一个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());        
} 

清除JLIST的最佳和最简单的方法是:

myJlist.setListData(new String[0]);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM