如何解决这个错误 "java.lang.ClassCastException: javax.swing.JList$3 cannot be cast to javax.swing.DefaultListModel"

我正在从下拉菜单中获取选择的值,并将其添加到jlist中。但只有我的字段(datepicker字段和另一个组合框字段)出现了这个错误。我初始化并使用了默认的列表模型。每次在添加之前,我都会清除我的列表。但当我在组合框中选择一个值时,它显示的错误是

4 Mar, 2014 7:39:23 AM adminscreen.taskallocationscreennew t_dateActionPerformed
SEVERE: null
java.lang.ClassCastException: javax.swing.JList$3 cannot be cast to javax.swing.DefaultListModel
    at adminscreen.taskallocationscreennew.t_dateActionPerformed(taskallocationscreennew.java:820)
    at adminscreen.taskallocationscreennew.access$600(taskallocationscreennew.java:50)
    at adminscreen.taskallocationscreennew$7.actionPerformed(taskallocationscreennew.java:579)
    at org.jdesktop.swingx.JXDatePicker.fireActionPerformed(JXDatePicker.java:876)
    at org.jdesktop.swingx.JXDatePicker.commitEdit(JXDatePicker.java:706)
    at org.jdesktop.swingx.plaf.basic.BasicDatePickerUI.commit(BasicDatePickerUI.java:901)
    at org.jdesktop.swingx.plaf.basic.BasicDatePickerUI$Handler.monthViewActionPerformed(BasicDatePickerUI.java:1374)
    at org.jdesktop.swingx.plaf.basic.BasicDatePickerUI$Handler.actionPerformed(BasicDatePickerUI.java:1349)
    at org.jdesktop.swingx.JXMonthView.fireActionPerformed(JXMonthView.java:1797)
    at org.jdesktop.swingx.JXMonthView.commitSelection(JXMonthView.java:1695)
    at org.jdesktop.swingx.plaf.basic.BasicMonthViewUI$Handler.mouseReleased(BasicMonthViewUI.java:2075)
    at java.awt.Component.processMouseEvent(Component.java:6267)
    at javax.swing.JComponent.processMouseEvent(JComponent.java:3267)
    at java.awt.Component.processEvent(Component.java:6032)
    at java.awt.Container.processEvent(Container.java:2041)
    at java.awt.Component.dispatchEventImpl(Component.java:4630)
    at java.awt.Container.dispatchEventImpl(Container.java:2099)
    at java.awt.Component.dispatchEvent(Component.java:4460)
    at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4577)
    at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4238)
    at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4168)
    at java.awt.Container.dispatchEventImpl(Container.java:2085)
    at java.awt.Window.dispatchEventImpl(Window.java:2478)
    at java.awt.Component.dispatchEvent(Component.java:4460)
    at java.awt.EventQueue.dispatchEvent(EventQueue.java:599)
    at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:269)
    at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:184)
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:174)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:169)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:161)
    at java.awt.EventDispatchThread.run(EventDispatchThread.java:122)

以下是我的代码。

   private void d_pagetypeActionPerformed(java.awt.event.ActionEvent evt)          {                                           
    // TODO add your handling code here:
   System.out.println("d_pagetype"+d_pagetype.getSelectedItem());

   try {

            if(l_storieslist.getModel() != null)
            {
        ((DefaultListModel) l_storieslist.getModel()).clear();
        listf(directoryName);
            }

            else
            {
            }
    } catch (Exception ex) {
        Logger.getLogger(taskallocationscreennew.class.getName()).log(Level.SEVERE, null, ex);
    }
    l_storieslist.setModel(fileNameList);
    l_storieslist.setBackground(Color.WHITE);

}

解决方案:

在下面一行。

l_storieslist.setModel(fileNameList);

fileNameList是JList的实例,它应该是DefaultListModel的实例。

可能你不需要再次设置模型。而是对现有的模型进行操作。你可以像你在代码中做的那样获得对现有模型的引用。

ListModel model = l_storieslist.getModel();

并执行添加和删除任何你想要的列表项的操作。

给TA打赏
共{{data.count}}人
人已打赏
未分类

如何选择不同的结果,然后对每个不同的结果执行多插入?

2022-9-9 23:28:40

未分类

在堆初始化过程中,文本大小递增

2022-9-9 23:28:42

0 条回复 A文章作者 M管理员
    暂无讨论,说说你的看法吧
个人中心
购物车
优惠劵
今日签到
有新私信 私信列表
搜索