Results 1 to 6 of 6
  1. #1
    ostkanten is offline Member
    Join Date
    Jul 2012
    Posts
    37
    Rep Power
    0

    Default Update JList problem

    This code:

    Java Code:
    private JList konton;
    List<Account> accounts = new ArrayList();
    ....
    public void updateAccountList(JList konton, List accounts){
    	    DefaultListModel listModel = (DefaultListModel)konton.getModel(); // line 711
    	    for(int i=0; i<accounts.size(); i++){
    	        listModel.addElement((String)accounts.get(i));
    	    }
    	    revalidate(); 	
    }
    gives error:

    Exception in thread "AWT-EventQueue-0" java.lang.ClassCastException: javax.swing.JList$4 cannot be cast to javax.swing.DefaultListModel
    at GUI.updateAccountList(GUI.java:711)
    at GUI$UpdateAccountListener.actionPerformed(GUI.java :259)

    I have read a lot at Internet about this problem but I still don´t know how to solve it? Any suggestions?

  2. #2
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,817
    Rep Power
    19

    Default Re: Update JList problem

    Clearly the ListModel is not a DefaultListModel but something internal to JList (that's what the $4 means, a class defined inside JList).
    Why not just use ListModel?
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  3. #3
    eRaaaa is offline Senior Member
    Join Date
    Oct 2010
    Location
    Germany
    Posts
    787
    Rep Power
    5

    Default Re: Update JList problem

    Your JList konton does not use a DefaultListModel as its model so you can`t cast it to a DefaultListModel :)!
    -> System.out.println(konton.getModel().getClass()); //prints javax.swing.JList$4 instead of DefaultListModel ?

    You have to create a DefaultListModel and set it as the model of your jlist.
    How do you create the jlist?

  4. #4
    ostkanten is offline Member
    Join Date
    Jul 2012
    Posts
    37
    Rep Power
    0

    Default Re: Update JList problem

    I did forget that. JList is created like

    konton=new JList(new DefaultListModel());

    Can I exclude Default and use ListModel to get it work?

  5. #5
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,188
    Rep Power
    19

    Default Re: Update JList problem

    To get better help sooner, post a SSCCE (Short, Self Contained, Correct (Compilable), Example) that demonstrates the problem. Not all your code, just a GUI with a JList constructed the way you are doing it, and the method that throws the Exception with maybe a JButton to invoke that method.

    Unrelated, but any class that has more than 711 lines probably could benefit from refactoring.

    Are you sure you didn't initialize the JList with an array?

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  6. #6
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,188
    Rep Power
    19

    Default Re: Update JList problem

    Quote Originally Posted by Tolls View Post
    Why not just use ListModel?
    addElement(...) is defined in DefaultListModel.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

Similar Threads

  1. Update Jlist in JFrame
    By dragon_baby in forum AWT / Swing
    Replies: 4
    Last Post: 05-26-2012, 02:07 PM
  2. JList problem?
    By tom2zip in forum AWT / Swing
    Replies: 2
    Last Post: 01-12-2012, 07:12 AM
  3. How update Jlist in java Chat
    By pappyj77 in forum Threads and Synchronization
    Replies: 59
    Last Post: 09-13-2011, 01:26 AM
  4. Cannot get my JList to update!
    By rangvald in forum AWT / Swing
    Replies: 1
    Last Post: 11-17-2009, 01:57 PM
  5. JList problem
    By zizou147 in forum Advanced Java
    Replies: 1
    Last Post: 04-17-2008, 08:50 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •