JList DefaultListModel ClassCastException

Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Aug 2007
Posts: 2
Reputation: yoyodelta is an unknown quantity at this point 
Solved Threads: 0
yoyodelta yoyodelta is offline Offline
Newbie Poster

JList DefaultListModel ClassCastException

 
0
  #1
Aug 13th, 2007
I cannot explain this problem, and in searching all over I've seen a few leads but no help. I do not understand at all why I get this exception. It claims I am trying to cast a JList as a DefaultListModel when I'me trying to cast a ListModel and a DefaultListModel (yes, I instantiated the JList with a DefaultListModel).


  1. Exception in thread "AWT-EventQueue-0" java.lang.ClassCastException: javax.swing.JList$4 cannot be cast to javax.swing.DefaultListModel
  2. at SchoolClassDisplay.actionPerformed(SchoolClassDisplay.java:171)
  3. at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
  4. at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
  5. at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
  6. at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
  7. at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source)
  8. at java.awt.Component.processMouseEvent(Unknown Source)
  9. at javax.swing.JComponent.processMouseEvent(Unknown Source)
  10. at java.awt.Component.processEvent(Unknown Source)
  11. at java.awt.Container.processEvent(Unknown Source)
  12. at java.awt.Component.dispatchEventImpl(Unknown Source)
  13. at java.awt.Container.dispatchEventImpl(Unknown Source)
  14. at java.awt.Component.dispatchEvent(Unknown Source)
  15. at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
  16. at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
  17. at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
  18. at java.awt.Container.dispatchEventImpl(Unknown Source)
  19. at java.awt.Window.dispatchEventImpl(Unknown Source)
  20. at java.awt.Component.dispatchEvent(Unknown Source)
  21. at java.awt.EventQueue.dispatchEvent(Unknown Source)
  22. at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
  23. at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
  24. at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
  25. at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
  26. at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
  27. at java.awt.EventDispatchThread.run(Unknown Source)

  1. import java.awt.GridBagConstraints;
  2. import java.awt.GridBagLayout;
  3. import java.awt.event.ActionListener;
  4. import java.awt.event.ActionEvent;
  5. import java.awt.GridLayout;
  6.  
  7. import javax.swing.JFrame;
  8. import javax.swing.JTextField;
  9. import javax.swing.JLabel;
  10. import javax.swing.JButton;
  11. import javax.swing.JSeparator;
  12. import javax.swing.JPanel;
  13. import javax.swing.JList;
  14. import javax.swing.WindowConstants;
  15. import javax.swing.DefaultListModel;
  16.  
  17. import java.util.ArrayList;
  18.  
  19. public class SchoolClassDisplay implements ActionListener {
  20.  
  21. private SchoolClass schoolClass; //class being edited
  22. private ActionListener ownerActionListener; //parent frame listener
  23. private String saveOptionActionCommand; //command for parent frame notification
  24. private String cancelOptionActionCommand; //command for parent frame notification
  25.  
  26. public SchoolClassDisplay (ActionListener ownerFrameActionListener, String saveClassCommand, String cancelClassCommand, SchoolClass schClass) throws NullPointerException {
  27. /*if (ownerFrameActionListener == null)
  28. throw new NullPointerException ("ownerFrameActionListener cannot be null");*/
  29. if (saveClassCommand == null)
  30. throw new NullPointerException ("saveTypeCommand cannot be null");
  31. if (cancelClassCommand == null)
  32. throw new NullPointerException ("cancelTypeCommand cannot be null");
  33.  
  34. schoolClass = schClass;
  35. ownerActionListener = ownerFrameActionListener;
  36. saveOptionActionCommand = saveClassCommand;
  37. cancelOptionActionCommand = cancelClassCommand;
  38. }
  39.  
  40. //GUI parts
  41. private JFrame frame;
  42. private JTextField name;
  43. private JTextField location;
  44. private JList times;
  45. private JButton addTime; //open add time slot prompt
  46. private JButton delTime;
  47. private JButton save;
  48. private JButton cancel;
  49.  
  50. private DayTimeDisplay dayTimeEditor; //time slot editor
  51. private String saveDayTimeActionCommand = "saveDayTime";//command linking above to this
  52.  
  53. public void show() {
  54.  
  55. GridBagLayout gridBag = new GridBagLayout();
  56. GridBagConstraints gbc = new GridBagConstraints();
  57. gbc.fill = GridBagConstraints.BOTH;
  58. gbc.anchor = GridBagConstraints.NORTHWEST;
  59.  
  60. frame = new JFrame ("Class Editor");
  61. frame.setLayout(gridBag);
  62. frame.setDefaultCloseOperation(WindowConstants.DO_NOTHING_ON_CLOSE);
  63. if (schoolClass != null)
  64. frame.setTitle(schoolClass.getName());
  65.  
  66. JLabel a = new JLabel("Name: ");
  67. gbc.gridwidth = 1;
  68. gridBag.setConstraints(a, gbc);
  69. frame.add(a);
  70. name = new JTextField("");
  71. if (schoolClass != null)
  72. name.setText(schoolClass.getName());
  73. gbc.weightx = 1;
  74. gbc.gridwidth = GridBagConstraints.REMAINDER;
  75. gridBag.setConstraints(name, gbc);
  76. frame.add(name);
  77.  
  78. a = new JLabel("Location: ");
  79. gbc.weightx = 0;
  80. gbc.gridwidth = 1;
  81. gridBag.setConstraints(a, gbc);
  82. frame.add(a);
  83. location = new JTextField("");
  84. if (schoolClass != null)
  85. location.setText(schoolClass.getLocation());
  86. gbc.weightx = 1;
  87. gbc.gridwidth = GridBagConstraints.REMAINDER;
  88. gridBag.setConstraints(location, gbc);
  89. frame.add(location);
  90.  
  91. a = new JLabel("Times: ");
  92. gbc.weightx = 0;
  93. gbc.gridwidth = 1;
  94. gridBag.setConstraints(a, gbc);
  95. frame.add(a);
  96. times = new JList(new DefaultListModel());
  97. if (schoolClass != null)
  98. times.setListData(schoolClass.getTimeSlots().toArray());
  99. gbc.weightx = 1;
  100. gbc.weighty = 1;
  101. gbc.gridwidth = GridBagConstraints.REMAINDER;
  102. gridBag.setConstraints(times, gbc);
  103. frame.add(times);
  104.  
  105. a = new JLabel(" ");
  106. gbc.weightx = 0;
  107. gbc.weighty = 1;
  108. gbc.gridwidth = 1;
  109. gridBag.setConstraints(a, gbc);
  110. frame.add(a);
  111. JPanel buttons1 = new JPanel(new GridLayout(1,2));
  112. gbc.weightx = 1;
  113. gbc.weighty = 1;
  114. gbc.gridwidth = GridBagConstraints.REMAINDER;
  115. gridBag.setConstraints(buttons1, gbc);
  116. frame.add(buttons1);
  117. addTime = new JButton("Add");
  118. addTime.addActionListener(this);
  119. addTime.setActionCommand("addTime");
  120. buttons1.add(addTime);
  121. delTime = new JButton("Delete");
  122. delTime.addActionListener(this);
  123. delTime.setActionCommand("delTime");
  124. buttons1.add(delTime);
  125.  
  126. JSeparator s = new JSeparator();
  127. gbc.weightx = 0;
  128. gbc.weighty = 0;
  129. gridBag.setConstraints(s, gbc);
  130. frame.add(s);
  131.  
  132. JPanel buttons2 = new JPanel(new GridLayout(1,2));
  133. gridBag.setConstraints(buttons2, gbc);
  134. frame.add(buttons2);
  135. save = new JButton("Save");
  136. save.addActionListener(this);
  137. save.addActionListener(ownerActionListener);
  138. save.setActionCommand(saveOptionActionCommand);
  139. buttons2.add(save);
  140. cancel = new JButton("Cancel");
  141. cancel.addActionListener(this);
  142. cancel.addActionListener(ownerActionListener);
  143. cancel.setActionCommand(cancelOptionActionCommand);
  144. buttons2.add(cancel);
  145.  
  146. JPanel blank = new JPanel();
  147. blank.setPreferredSize(new java.awt.Dimension(0,0));
  148. gbc.weightx = 1;
  149. gbc.weighty = 1;
  150. gbc.gridwidth = GridBagConstraints.REMAINDER;
  151. gbc.gridheight = GridBagConstraints.REMAINDER;
  152. gridBag.setConstraints(blank,gbc);
  153. frame.add(blank);
  154.  
  155.  
  156. frame.pack();
  157. frame.setVisible(true);
  158. }
  159.  
  160. public void actionPerformed (ActionEvent a) {
  161. //open add time slot dialogue
  162. if (a.getActionCommand().equals(addTime.getActionCommand())) {
  163. frame.setEnabled(false);
  164. dayTimeEditor = new DayTimeDisplay(this, saveDayTimeActionCommand, null);
  165. dayTimeEditor.show();
  166. }
  167. //remove time slot
  168. if (a.getActionCommand().equals(delTime.getActionCommand())) {
  169. if (times.getSelectedIndex() != -1) {
  170. ((DefaultListModel)(times.getModel())).remove(times.getSelectedIndex());
  171. }
  172. }
  173. //command received from time slot editor indicating "Add" pressed finalizing creation
  174. if (a.getActionCommand().equals(saveDayTimeActionCommand)) {
  175. frame.setEnabled(true);
  176. frame.requestFocus();
  177. DayTime oldDayTime = dayTimeEditor.getUneditedAssignment(); //store old if was editing mode
  178. DayTime newDayTime = dayTimeEditor.getSavedAssignment();
  179.  
  180. if(newDayTime == null) //failed creation
  181. return;
  182.  
  183. if (oldDayTime == null)
  184. ((DefaultListModel)(times.getModel())).addElement(newDayTime);
  185. else {
  186. int index = ((DefaultListModel)(times.getModel())).indexOf(oldDayTime);
  187. ((DefaultListModel)(times.getModel())).set(index, newDayTime);
  188. }
  189. }
  190. }
  191.  
  192. //test purposes only
  193. public static void main (String[] args) {
  194.  
  195. ArrayList<DayTime> daytimes = new ArrayList<DayTime>();
  196. int[] days1 = {0,1,0,1,0,1,0};
  197. int[] days2 = {0,0,1,0,1,0,0};
  198. daytimes.add(new DayTime(days1,10,30));
  199. daytimes.add(new DayTime(days2,14,30));
  200. SchoolClass associatedClass1 = new SchoolClass("Math","School",daytimes);
  201.  
  202.  
  203. SchoolClassDisplay a = new SchoolClassDisplay (null, "saveClass", "cancelClass", associatedClass1);
  204. a.show();
  205. }
  206. }


I tried to reproduce this problem in a very simple setting; the same basic thing is happening, lines 11 and 171 are identical and set up the same way. However, this brought NO exception. What is going on?

  1. import javax.swing.*;
  2.  
  3. public class dfgdrtgrtg {
  4.  
  5. private JList times;
  6.  
  7. public dfgdrtgrtg () {
  8.  
  9. times = new JList(new DefaultListModel());
  10.  
  11. DefaultListModel listModel = (DefaultListModel)(times.getModel());
  12.  
  13. }
  14.  
  15. public static void main (String[] args) {
  16.  
  17. dfgdrtgrtg a = new dfgdrtgrtg();
  18.  
  19.  
  20. }
  21. }

Thanks for the help.
Reply With Quote Quick reply to this message  
Join Date: Aug 2007
Posts: 2
Reputation: yoyodelta is an unknown quantity at this point 
Solved Threads: 0
yoyodelta yoyodelta is offline Offline
Newbie Poster

Re: JList DefaultListModel ClassCastException

 
0
  #2
Aug 14th, 2007
Finally figured out the problem:

When I called times.setListData(Object[] array)
JList constructs a new instance of the ListModel based on its own internal, incompatible with DefaultListModel, ListModel; as if I never used the constructor passing in a DefaultListModel.
Data then must be added one at a time.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:




Views: 2861 | Replies: 1
Thread Tools Search this Thread



Tag cloud for Java
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC