Results 1 to 6 of 6
Thread: Update JList problem
- 09-24-2012, 04:13 PM #1
Member
- Join Date
- Jul 2012
- Posts
- 37
- Rep Power
- 0
Update JList problem
This code:
gives error: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(); }
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?
- 09-24-2012, 04:35 PM #2
Moderator
- Join Date
- Apr 2009
- Posts
- 10,481
- Rep Power
- 16
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.
- 09-24-2012, 04:38 PM #3
Senior Member
- Join Date
- Oct 2010
- Location
- Germany
- Posts
- 780
- Rep Power
- 4
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?
- 09-24-2012, 04:48 PM #4
Member
- Join Date
- Jul 2012
- Posts
- 37
- Rep Power
- 0
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?
- 09-25-2012, 04:50 AM #5
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?
dbWhy do they call it rush hour when nothing moves? - Robin Williams
- 09-25-2012, 04:53 AM #6
Similar Threads
-
Update Jlist in JFrame
By dragon_baby in forum AWT / SwingReplies: 4Last Post: 05-26-2012, 02:07 PM -
JList problem?
By tom2zip in forum AWT / SwingReplies: 2Last Post: 01-12-2012, 07:12 AM -
How update Jlist in java Chat
By pappyj77 in forum Threads and SynchronizationReplies: 59Last Post: 09-13-2011, 01:26 AM -
Cannot get my JList to update!
By rangvald in forum AWT / SwingReplies: 1Last Post: 11-17-2009, 01:57 PM -
JList problem
By zizou147 in forum Advanced JavaReplies: 1Last Post: 04-17-2008, 08:50 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks