JList adding and removing items (Netbeans)
Solution 1
You set the model of the list to an AbstractListModel
. You can't cast the model to a DefaultListModel
. Trying to do so will give you a ClassCastException
So set the model to a DefaultListModel
instead.
jList1.setModel(new DefaultListModel());
And you probably want to use DefaultListModel#addElement(element)
instead of adding the element to same index every time, with add(2, element)
Solution 2
this is how I declared the jList:
Why are you creating a custom ListModel? Just use the DefaultListModel. There is no need for you to create a custom model to simply store String data.
Then you can read the section from the Swing tutorial on How to Use Lists for a working example that does exactly what you want by using the "Hire" and "Fire" buttons.
arnoutvh
Updated on July 09, 2022Comments
-
arnoutvh almost 2 years
I'm trying to add and remove items from my jList (jList1), but It doesn't work. I've searched on stackoverflow for other people with the same problem, but when their problem is solved, I keep getting errors. So this is how I declared the jList:
jList1.setModel(new javax.swing.AbstractListModel() { String [] strings = lijstItems; public int getSize() { return strings.length; } public Object getElementAt (int i) { return strings[i]; } });
So now I made these buttons to add and remove items from the list:
private void addHostActionPerformed(java.awt.event.ActionEvent evt) { // TODO add your handling code here: DefaultListModel model = (DefaultListModel) jList1.getModel(); model.add(2, "item"); // THIS DOES NOT WORK... }
And
private void deleteHostActionPerformed(java.awt.event.ActionEvent evt) { // TODO add your handling code here: }
I've tried so many things, but they don't work! Can anyone help me please?
Thanks!
-
arnoutvh about 10 yearsOkay, but when I set the model to an AbstractListModel, I can't use the model.addList method? AbstractListModel model = (AbstractListModel) jList1.getModel(); model.addList(index,"string"); //addList gives an error
-
Paul Samsotha about 10 yearsI didn't tell you do cast to
AbstractListModel
. I said the set the model to aDefaultListModel
.AbstractListeModel
has no data manipulation methods -
arnoutvh about 10 yearsSo if I have to set the model to
AbstractListModel
and cast it toDefaultListModel
, you say it should work? But when I try, I can't use the.addElement
-
Paul Samsotha about 10 yearsNO That is what you are already doing. And it's not working. If you're using Netbeans GUI Builder, just right click on the list, and go to properties. Click the ellipses in the
model
property. Go tocustom code
from the drop down and just type innew DefaultListModel()
. You'll probably have to resolve imports afterward. Just go to your source and hit Ctrl+Shift+O -
arnoutvh about 10 yearsThere are no 'ellipses' in the model property. I'm running NetBeans 7.3.1 for Mac OS X.
-
Paul Samsotha about 10 yearsIs there not a small little button on the very right of the property that you can click
-
arnoutvh about 10 yearsof course there is, but that's for editing the items manually.
-
Paul Samsotha about 10 yearsThe dialog you're referring to is where the drop down I'm talking about. If it's not there, then I don't know, must be a mac thing. You can then just set the model in the constructor instead, after initComponents()
-
Paul Samsotha about 10 yearsEven when you select "custom code"? If you can't then just do what I said an type in the constructor, after
initComponents()
in the source code.jList1.setModel(new javax.swing.DefaultListModel());
.Then -
arnoutvh about 10 yearsI think I changed it, but when I click the button, nothing happens.
-
Paul Samsotha about 10 yearsI couldn't tell you what you're doing wrong then, without seeing more code. If you set the model to DefautListModel and in you actionPerformed you're doing
DefaultListModel model = (DefaultListModel)jList1.getModel(); model.addElement("Hello");
, There's no reason it shouldn't work