This discussion is archived
8 Replies Latest reply: Aug 19, 2008 2:48 PM by DrClap RSS

DefaultListModel casting problem

807589 Newbie
Currently Being Moderated
I'm not really sure what's going on here. I've got 2 JListBox controls, listPhotos and listOtherFiles. I'm trying to get the models for them so I can clear whats in them. Here's the section of code I've marked the line that is showing the area.
        
        if(iPersonType != 3) {
            DefaultTableModel relModel = (DefaultTableModel) tblRelations.getModel();
            relModel.setColumnCount(3);
            Object rlIDs[] = {"Last Name", "First Name", "Relation"};
            relModel.setColumnIdentifiers(rlIDs);

*           DefaultListModel picModel = (DefaultListModel)listPhotos.getModel();</div></div>
            picModel.removeAllElements();


            DefaultListModel oflModel = (DefaultListModel) listOtherFiles.getModel();
            oflModel.removeAllElements();

        }
Here is the exception
Exception in thread "AWT-EventQueue-0" java.lang.ClassCastException: bondtracker.resources.NewPerson$3 cannot be cast to javax.swing.DefaultListModel
This file is bondtracker.resources.NewPerson.java and I have an import for javax.swing.DefaultListModel.
this also happens with the next similar code referring to oflModel. Any ideas what I'm missing here?
  • 1. Re: DefaultListModel casting problem
    807589 Newbie
    Currently Being Moderated
    Myself, I can't really tell where your problem is without more code. You may wish to create a Short, Self Contained, Correct (Compilable), Example or SSCCE. This is a small application that you create that is compilable and runnable, and demonstrates your error, but contains no extraneous, unnecessary code that is not associated with your problem. To see more about this and how to create this, please look at this link:

    [http://homepage1.nifty.com/algafield/sscce.html|http://homepage1.nifty.com/algafield/sscce.html]

    But also a question: When you created these JLists (what's a JListBox?), how did you create the models? Did you create a DefaultListModel to begin with?
  • 2. Re: DefaultListModel casting problem
    807589 Newbie
    Currently Being Moderated
    I didn't specify the model when i created them. I'm using netbeans 6.0 for my IDE. and i typo'd they are JList controls. My JTable controls work just fine.

    Here's a link to the full java code.

    [http://www.stormguardsolutions.net/NewPerson.java]
  • 3. Re: DefaultListModel casting problem
    807589 Newbie
    Currently Being Moderated
    IceDownjb wrote:
    I didn't specify the model when i created them. I'm using netbeans 6.0 for my IDE. and i typo'd they are JList controls. My JTable controls work just fine.
    Specifying the model will likely fix this.
    Here's a link to the full java code.
    I don't want to see the full Java code, nor do I wish to have to link to it. If this suggestion above doesn't help you, then I again recommend that you create and post here an SSCCE. Good luck.
  • 4. Re: DefaultListModel casting problem
    807589 Newbie
    Currently Being Moderated
    Here's my SSCCE where I create two JLists, listA which initializes itself with a String array and listB which initializes itself with a DefaultListModel that was filled with data from the same String array. Trying to cast the listA model to a DefaultListModel and then clearing it (the preferred method to clear the data from it) throws the exception. listB works fine.
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    
    import javax.swing.DefaultListModel;
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JList;
    import javax.swing.JPanel;
    import javax.swing.JScrollPane;
    
    public class TestListModel extends JPanel
    {
      
      public TestListModel()
      {
        String[] texts = {"Fe", "Fi", "Fo", "Fum"};
        final DefaultListModel modelB = new DefaultListModel();
        for (int i = 0; i < texts.length; i++)
        {
          modelB.addElement(texts);
    }

    // listA initializes with an array of Strings
    final JList listA = new JList(texts);

    // listB initializes with a DefaultListModel
    final JList listB = new JList(modelB);
    add(new JScrollPane(listA));
    add(new JScrollPane(listB));

    JButton btnA = new JButton("Clear A");
    add(btnA);
    btnA.addActionListener(new ActionListener()
    {
    public void actionPerformed(ActionEvent arg0)
    {
    DefaultListModel model = (DefaultListModel)listA.getModel();
    model.clear();
    // this throws a class cast exception because the model is
    // not a DefaultListModel
    }
    });

    JButton btnB = new JButton("Clear B");
    add(btnB);
    btnB.addActionListener(new ActionListener()
    {
    public void actionPerformed(ActionEvent arg0)
    {
    DefaultListModel model = (DefaultListModel)listB.getModel();
    model.clear(); // this works fine
    }
    });

    }

    private static void createAndShowGUI()
    {
    JFrame frame = new JFrame("TestListModel Application");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.getContentPane().add(new TestListModel());
    frame.pack();
    frame.setLocationRelativeTo(null);
    frame.setVisible(true);
    }

    public static void main(String[] args)
    {
    javax.swing.SwingUtilities.invokeLater(new Runnable()
    {
    public void run()
    {
    createAndShowGUI();
    }
    });

    }
    }
  • 5. Re: DefaultListModel casting problem
    807589 Newbie
    Currently Being Moderated
    Set the model and am still having the same problem, here's an abridged version of the code:
    package bondtracker.resouces;
    
    import javax.swing.DefaultListModel;
    import javax.swing.JList;
    
    public class NewPerson extends javax.swing.JDialog {
    
        private javax.swing.JList listPhotos;
    
        public NewPerson(java.awt.Frame parent, boolean modal) {
            super(parent, modal);
            initComponents();
            initView();
            
        }
    
        public void initComponents() {
            listPhotos = new javax.swing.JList();
            listPhotos.setModel(new DefaultListModel());
            listPhotos.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
            listPhotos.setName("listPhotos"); // NOI18N
            jScrollPane7.setViewportView(listPhotos);
        }
    
        private void initView() {
                DefaultListModel picModel = (DefaultListModel)listPhotos.getModel();
                picModel.removeAllElements();
        }
    }
  • 6. Re: DefaultListModel casting problem
    DrClap Expert
    Currently Being Moderated
    All you need to do is to look at the anonymous inner class inside bondtracker.resources.NewPerson which you attached to that List as its ListModel. You will find it's not a DefaultListModel.

    Fix the problem either by (1) making it a DefaultListModel or (2) not casting it to a DefaultListModel.
  • 7. Re: DefaultListModel casting problem
    807589 Newbie
    Currently Being Moderated
    Ok, I've got it working now, That was it, just because of a netbeans thing, it wasn't putting the model i specified in right when I tried it the first time. Thnx
  • 8. Re: DefaultListModel casting problem
    DrClap Expert
    Currently Being Moderated
    IceDownjb wrote:
    Set the model and am still having the same problem, here's an abridged version of the code:
    I stuck a main() method into that code and had it create an instance using that constructor. Didn't have a problem. What error message did you get when you ran that code?