在我的应用程序中,我已将TableRowSorter应用于我的JTable行排序工作正常,给出了行排序器的代码。
TableRowSorter<TableModel> sorter = new TableRowSorter<TableModel>(tableModel);
tableMain.setRowSorter(sorter);
我的应用程序中有JTree和JTable,JTree包含文件夹,Jtable显示来自JTree Node的所选文件夹的数据(文件和子文件夹信息)。当我从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排序任何colum并回到JTree并点击其他节点查看其详细信息然后发生异常并且JTree是hide异常时,问题出现了
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)
答案 0 :(得分:3)
如How to Use Tables: Sorting and Filtering中所述,JTable
视图坐标 与TableModel
模型坐标相同。 JTable
提供了在两者之间进行转换的方法,在此上下文中可能需要这些方法。一个症状是通过拖动无法正确处理重新排序列的表。
答案 1 :(得分:0)
尝试:
table.convertColumnIndexToModel(viewColumnIndex)
table.convertRowIndexToModel(viewRowIndex)
检索所需的实际行或列。