简体   繁体   English

Java Swing类无法转换为DefaultListModel

[英]Java Swing class cannot be cast into DefaultListModel

I'm working on a project with Swing and I have the following event handler: 我正在使用Swing进行项目,并且具有以下事件处理程序:

private void txtInputKeyPressed(java.awt.event.KeyEvent evt) {                                    

    if (evt.getKeyCode() == ENTER) {
        DefaultListModel listModel = (DefaultListModel) lFrequencyTable.getModel();

        listModel.removeAllElements();

        ArrayList<FrequencyChar> table = 
                engine.computeFrequencyTable(txtInput.getText());

        for (FrequencyChar f: table) {
            listModel.addElement(f.character);
        }
    }
} 

A lot of the code is auto generated from Netbeans which is why I haven't posted all the UI code. Netbeans会自动生成很多代码,这就是为什么我没有发布所有UI代码的原因。 I'm getting an exception when I try to initialize listModel: Exception in thread "AWT-EventQueue-0" java.lang.ClassCastException: brutus.BrutusUI$2 cannot be cast to javax.swing.DefaultListModel . 我尝试初始化listModel时遇到异常: Exception in thread "AWT-EventQueue-0" java.lang.ClassCastException: brutus.BrutusUI$2 cannot be cast to javax.swing.DefaultListModel BrutusUI is the swing UI class generated by NetBeans (it's just the main entry point of the app with all the components). BrutusUI是NetBeans生成的swing UI类(它只是具有所有组件的应用程序的主要入口点)。 However, NetBeans is showing me that the return value of lFrequencyTable.getModel(); 但是,NetBeans向我展示了lFrequencyTable.getModel();的返回值lFrequencyTable.getModel(); is a ListModel so why would it be trying to cast the UI class as the DefaultListModel? ListModel ,为什么要尝试将UI类转换为DefaultListModel?

它不是试图将BrutusUI转换为DefaultListModel ,而是brutus.BrutusUI$2 ,其中$2表示在类BrutusUI声明的匿名类(实现ListModel )。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM