JList adding and removing items (Netbeans)

25,824

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.

Share:
25,824
arnoutvh
Author by

arnoutvh

Updated on July 09, 2022

Comments

  • arnoutvh
    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
    arnoutvh about 10 years
    Okay, 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
    Paul Samsotha about 10 years
    I didn't tell you do cast to AbstractListModel. I said the set the model to a DefaultListModel. AbstractListeModel has no data manipulation methods
  • arnoutvh
    arnoutvh about 10 years
    So if I have to set the model to AbstractListModel and cast it to DefaultListModel, you say it should work? But when I try, I can't use the .addElement
  • Paul Samsotha
    Paul Samsotha about 10 years
    NO 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 to custom code from the drop down and just type in new DefaultListModel(). You'll probably have to resolve imports afterward. Just go to your source and hit Ctrl+Shift+O
  • arnoutvh
    arnoutvh about 10 years
    There are no 'ellipses' in the model property. I'm running NetBeans 7.3.1 for Mac OS X.
  • Paul Samsotha
    Paul Samsotha about 10 years
    Is there not a small little button on the very right of the property that you can click
  • arnoutvh
    arnoutvh about 10 years
    of course there is, but that's for editing the items manually.
  • Paul Samsotha
    Paul Samsotha about 10 years
    The 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
    Paul Samsotha about 10 years
    Even 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
    arnoutvh about 10 years
    I think I changed it, but when I click the button, nothing happens.
  • Paul Samsotha
    Paul Samsotha about 10 years
    I 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