org.jdesktop.swingbinding.JTableBinding$BindingTableModel cannot be cast to javax.swing.table.DefaultTableModel

纵然是瞬间 提交于 2019-12-31 04:40:06

问题


I tried to group JTable header and for that I want to get the DefaultTableModel of current JTable. But when I tried to retrieve the table model like this:

DefaultTableModel dm=(DefaultTableModel) tblTet.getModel();

It shows the following exception:

org.jdesktop.swingbinding.JTableBinding$BindingTableModel cannot be cast to javax.swing.table.DefaultTableModel

Because I am using JTableBeansBinding.

Does anyone know how to solve this problem (for retrieving DefaultTableModel)?


回答1:


According to the JavaDoc for BindingTableModel, the class doesn't extend DefaultTableModel. Rather, it implements TableModel interface. This means that you cannot cast to DefaultTableModel, only to TableModel:

TableModel dm=(TableModel) tblTet.getModel();



回答2:


you have to create your table like this :

    String[] columnNames = {"Row",
            "Category",
            "From Date",
            "From Time",
            "To Date",
            "To Time",
            "Description",
            "Doc"};
    Object[][] data = {};//Table Rows

   table.setModel(new DefaultTableModel(data,columnNames));

instead of:

   table=new JTable(data,columnNames);

then you can cast your table model to defaultTableModel.



来源:https://stackoverflow.com/questions/10131878/org-jdesktop-swingbinding-jtablebindingbindingtablemodel-cannot-be-cast-to-java

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!