简体   繁体   中英

JList how to remove an Item in simple way?

I know there are some questions related to this but they did not help me at all

my code is simple I'm trying to delete the first item on my list

DefaultListModel model = (DefaultListModel) jList1.getModel();        
model.removeElementAt(0);

this gives me a ClassCastException as follows

Exception in thread "AWT-EventQueue-0" java.lang.ClassCastException: rfs.search$1 cannot be cast to javax.swing.DefaultListModel
at rfs.search.jTextField1KeyReleased(search.java:130)
at rfs.search.access$500(search.java:15)

'rfs' is my package name and the 'search.java' is java file that contains the jList1

basically my code look like this (there are some netbeans auto generated code I did not include here)

package rfs;

import javax.swing.DefaultListModel;


public class search extends javax.swing.JFrame {
    public search() {
        initComponents();

}
private void jTextField1KeyReleased(java.awt.event.KeyEvent evt){                                        
    DefaultListModel dlm = (DefaultListModel) jList1.getModel();        
    dlm.removeElementAt(0);

}                                      

// Variables declaration - do not modify                     
private javax.swing.JList jList1;
private javax.swing.JScrollPane jScrollPane1;
private javax.swing.JTextField jTextField1;
// End of variables declaration                   

}

If you initialize your JList using Vector , all changes to the Vector will be visible in the JList .

Code:

Vector<String> vector = new Vector<>();
vector.add("a");
vector.add("b");
vector.add("c");
JList<String> jlist = new JList<>(vector);

jlist.setSelectedIndices(new int[] {0, 1, 2});
System.out.println(jlist.getSelectedValuesList());

vector.remove(0);

jlist.setSelectedIndices(new int[] {0, 1, 2});
System.out.println(jlist.getSelectedValuesList());

Output:

[a, b, c]
[b, c]

There are two ways but both are similar: Way 1 : Suppose your JList is jList1 now to use DefaultListModel in your jList1 you need to set jList1 model, follow the code to set model and add values in your jList1:

jList1.setModel(new DefaultListModel());

DefaultListModel lm1=(DefaultListModel) jList1.getModel();

lm1.add(0, "A");
lm1.add(1, "B");
lm1.add(2, "C");
lm1.add(3, "D");
lm1.add(4, "E");

To remove first(0) item follow the code:

lm1.remove(0);

Way 2 : In NetBeans follow those steps: 1st in drag and drop area select your JList and right click and select "Customize Code..." then on left side change "default code" to "custom creation" and change the code(on right side) according to the following code,

jList2 = new javax.swing.JList();
jList2.setModel(new DefaultListModel());
jScrollPanel.setViewportView(jList2);

Here jList2 is variable name of JList you are using

Now you can use DefaultListModel without any exception. You can add values to the list following way

DefaultListModel listModel=(DefaultListModel)jList2.getModel();
listModel.add(0,"A");
listModel.add(1,"B");
listModel.add(2,"C");

and to remove first(0) item just do listModel.remove(0);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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