0

在我的应用程序中,我已将 TableRowSorter 应用于我的 JTable 行排序工作正常,给出了行排序器的代码。

TableRowSorter<TableModel> sorter = new TableRowSorter<TableModel>(tableModel);
tableMain.setRowSorter(sorter);

我的应用程序中有 JTree 和 JTable,JTree 包含文件夹,而 Jtable 显示来自 JTree 节点的选定文件夹的数据(文件和子文件夹信息)。当我单击 JTree 中的节点时,它将正确填充 JTable 填充 JTable 的代码如下所示

public void populateTable(Vector<FileData> vList)
{
    treePath = treeMainView.getSelectionPath();

    FileData metaData;
    ImageIcon icon = new ImageIcon("images/folder.png");

    dtModel.getDataVector().removeAllElements();

    for (int i = 0; i < vList.size(); i++)
    {
        Vector v = new Vector();
        metaData = (FileData) vList.get(i);
        if (metaData.isIsdirectory()) {
            v.add(icon);
        } else {
            v.add(UtilClass.getIcon(metaData.getClientpath()));
        }
        v.add(metaData.getName());
        v.add(metaData.getClientpath());
        if (metaData.isIsdirectory())
        {
            v.add("");
        }
        else
        {
            v.add((FileHelper.getSizeString(metaData.getSize())));
        }
        if (metaData.isIsdirectory()) {
            v.add("");
        }
        else
        {
            v.add(new Date(metaData.getTime()));
        }
        if (metaData.isIsdirectory()) {
            v.add("Folder");
        } 
        else
        {
            v.add(UtilClass.getIcon(metaData.getClientpath()));
        }



        tableModel.insertRow(i, v);

    }
    tableMain.repaint();
}

当我对 JTable 中的任何列进行排序并返回 JTree 并单击其他节点以查看其详细信息时,就会出现此问题,然后将发生异常并且 JTree 隐藏异常如下所示

Exception in thread "AWT-EventQueue-0" java.lang.ArrayIndexOutOfBoundsException: 4 >= 1
at java.util.Vector.elementAt(Vector.java:427)
at javax.swing.table.DefaultTableModel.getValueAt(DefaultTableModel.java:632)
at javax.swing.table.TableRowSorter$TableRowSorterModelWrapper.getValueAt(TableRowSorter.java:252)
at javax.swing.table.TableRowSorter$TableRowSorterModelWrapper.getStringValueAt(TableRowSorter.java:268)
at javax.swing.DefaultRowSorter.compare(DefaultRowSorter.java:935)
at javax.swing.DefaultRowSorter.access$100(DefaultRowSorter.java:95)
at javax.swing.DefaultRowSorter$Row.compareTo(DefaultRowSorter.java:1359)
at javax.swing.DefaultRowSorter$Row.compareTo(DefaultRowSorter.java:1349)
at java.util.Arrays.binarySearch0(Arrays.java:2001)
at java.util.Arrays.binarySearch(Arrays.java:1943)
at javax.swing.DefaultRowSorter.insertInOrder(DefaultRowSorter.java:983)
at javax.swing.DefaultRowSorter.rowsInserted0(DefaultRowSorter.java:1041)
at javax.swing.DefaultRowSorter.rowsInserted(DefaultRowSorter.java:851)
at javax.swing.JTable.notifySorter(JTable.java:4267)
at javax.swing.JTable.sortedTableChanged(JTable.java:4115)
at javax.swing.JTable.tableChanged(JTable.java:4392)
at javax.swing.table.AbstractTableModel.fireTableChanged(AbstractTableModel.java:280)
at javax.swing.table.AbstractTableModel.fireTableRowsInserted(AbstractTableModel.java:215)
at javax.swing.table.DefaultTableModel.insertRow(DefaultTableModel.java:359)
at org.mycom.gui.MyClass.populateTable(MyClass.java:5866)
at org.mycom.gui.MyClass$Selection.valueChanged(MyClass.java:5753)
at javax.swing.JTree.fireValueChanged(JTree.java:2820)
at javax.swing.JTree$TreeSelectionRedirector.valueChanged(JTree.java:3191)
at javax.swing.tree.DefaultTreeSelectionModel.fireValueChanged(DefaultTreeSelectionModel.java:629)
at javax.swing.tree.DefaultTreeSelectionModel.notifyPathChange(DefaultTreeSelectionModel.java:1078)
at javax.swing.tree.DefaultTreeSelectionModel.setSelectionPaths(DefaultTreeSelectionModel.java:287)
at javax.swing.tree.DefaultTreeSelectionModel.setSelectionPath(DefaultTreeSelectionModel.java:170)
at javax.swing.JTree.setSelectionPath(JTree.java:1598)
at javax.swing.plaf.basic.BasicTreeUI.selectPathForEvent(BasicTreeUI.java:2316)
at javax.swing.plaf.basic.BasicTreeUI$Handler.handleSelection(BasicTreeUI.java:3525)
at javax.swing.plaf.basic.BasicTreeUI$Handler.mousePressed(BasicTreeUI.java:3464)
at java.awt.AWTEventMulticaster.mousePressed(AWTEventMulticaster.java:262)
at java.awt.Component.processMouseEvent(Component.java:6213)
at javax.swing.JComponent.processMouseEvent(JComponent.java:3265)
at java.awt.Component.processEvent(Component.java:5981)
at java.awt.Container.processEvent(Container.java:2041)
at java.awt.Component.dispatchEventImpl(Component.java:4583)
at java.awt.Container.dispatchEventImpl(Container.java:2099)
at java.awt.Component.dispatchEvent(Component.java:4413)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4556)
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4217)
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4150)
at java.awt.Container.dispatchEventImpl(Container.java:2085)
at java.awt.Window.dispatchEventImpl(Window.java:2475)
at java.awt.Component.dispatchEvent(Component.java:4413)
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)
4

2 回答 2

3

正如如何使用表格:排序和过滤中所讨论的,JTable视图坐标TableModel模型坐标不同。JTable提供在两者之间转换的方法,在这种情况下可能需要。一个症状是表格无法通过拖动正确处理重新排序的列。

于 2012-04-24T01:48:03.280 回答
0

尝试:

    table.convertColumnIndexToModel(viewColumnIndex)
    table.convertRowIndexToModel(viewRowIndex)

检索您想要的实际行或列。

于 2014-02-25T14:44:46.237 回答