JXTable 和 JTable 的 Swingtools 扩展,隐藏列 return 索引越界
Swingtools extension of JXTable and JTable, hiding columns return index out of bounds
我正在评估 Swingtools JTable,它是 JTable 和 JXTable 的开源且功能更强大的版本。正如维护者 Ivan Portyankin 在他的博客 ipsoftware blog:
中解释的那样
"Sometimes it may seem that standard Swing JTable or its close
companions like SwingX JXTable are too limited. You don’t have too
many funny ways to manipulate them – they are strict rectangular
grids, with every row taking fixed amount of screen space in pixels
and columns taken from one and only column model. Columns cannot be
spanned or split and always take a single cell in the grid."
现在我正在研究隐藏列的能力,这可以通过其界面完成:但是我想默认隐藏一些列并在用户手动重新启用时存储首选项他们总是显示最后选择的偏好。
下面我尝试默认隐藏所有列,但出现 ArrayIndexOutOfBoundsException
错误!!
public class AdvancedTableDemo extends JFrame
{
public AdvancedTableDemo()
{
super("Advanced Table Demo");
setDefaultCloseOperation(EXIT_ON_CLOSE);
setSize(600, 300);
final BaseTable baseTable = new BaseTable();
baseTable.setFilterHeaderEnabled(true);
add(new JScrollPane(baseTable));
BeanPropertyTableModel<TableBean> model = new BeanPropertyTableModel<TableBean>(TableBean.class);
model.setOrderedProperties(Arrays.asList("name", "surname", "date"));
model.setData(TableBean.generateList(100));
baseTable.setModel(model);
int j = baseTable.getColumnCount(true);
for (int i = 0; i < j; i++)
{
TableColumnExt tableColumnExt = baseTable.getColumnExt(i);
tableColumnExt.setVisible(false);
}
setVisible(true);
}
public static void main(String[] args)
{
SwingUtilities.invokeLater(new Runnable()
{
@Override
public void run()
{
new AdvancedTableDemo();
}
});
}
}
错误是:
Exception in thread "AWT-EventQueue-0" java.lang.ArrayIndexOutOfBoundsException: 2 >= 1
at java.util.Vector.elementAt(Vector.java:474)
at javax.swing.table.DefaultTableColumnModel.getColumn(DefaultTableColumnModel.java:294)
at org.jdesktop.swingx.JXTable.getColumn(JXTable.java:2265)
at org.jdesktop.swingx.JXTable.getColumnExt(JXTable.java:2415)
at demo.table.AdvancedTableDemo.<init>(AdvancedTableDemo.java:46)
at demo.table.AdvancedTableDemo.run(AdvancedTableDemo.java:62)
at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:311)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:756)
at java.awt.EventQueue.access0(EventQueue.java:97)
at java.awt.EventQueue.run(EventQueue.java:709)
at java.awt.EventQueue.run(EventQueue.java:703)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:726)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:201)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:116)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:105)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:93)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:82)
列数为 3,但大于第 1 列的任何值都会产生刚刚看到的错误!尝试使用 17 列和任何大于 10 returns 的异常。
我是不是遗漏了什么??
The column count is 3, but getting anything greater than column 1 yields the error
int j = baseTable.getColumnCount(true);
for (int i = 0; i < j; i++)
是的,但是隐藏第一列后只有 2 列。然后隐藏下一列后只有一列(因此出现异常)。
所以解决方案是隐藏 table 末尾的列,而不是开头:
int j = baseTable.getColumnCount(true) -1;
for (int i = j; i >= 0; i--)
我正在评估 Swingtools JTable,它是 JTable 和 JXTable 的开源且功能更强大的版本。正如维护者 Ivan Portyankin 在他的博客 ipsoftware blog:
中解释的那样"Sometimes it may seem that standard Swing JTable or its close companions like SwingX JXTable are too limited. You don’t have too many funny ways to manipulate them – they are strict rectangular grids, with every row taking fixed amount of screen space in pixels and columns taken from one and only column model. Columns cannot be spanned or split and always take a single cell in the grid."
现在我正在研究隐藏列的能力,这可以通过其界面完成:
下面我尝试默认隐藏所有列,但出现 ArrayIndexOutOfBoundsException
错误!!
public class AdvancedTableDemo extends JFrame
{
public AdvancedTableDemo()
{
super("Advanced Table Demo");
setDefaultCloseOperation(EXIT_ON_CLOSE);
setSize(600, 300);
final BaseTable baseTable = new BaseTable();
baseTable.setFilterHeaderEnabled(true);
add(new JScrollPane(baseTable));
BeanPropertyTableModel<TableBean> model = new BeanPropertyTableModel<TableBean>(TableBean.class);
model.setOrderedProperties(Arrays.asList("name", "surname", "date"));
model.setData(TableBean.generateList(100));
baseTable.setModel(model);
int j = baseTable.getColumnCount(true);
for (int i = 0; i < j; i++)
{
TableColumnExt tableColumnExt = baseTable.getColumnExt(i);
tableColumnExt.setVisible(false);
}
setVisible(true);
}
public static void main(String[] args)
{
SwingUtilities.invokeLater(new Runnable()
{
@Override
public void run()
{
new AdvancedTableDemo();
}
});
}
}
错误是:
Exception in thread "AWT-EventQueue-0" java.lang.ArrayIndexOutOfBoundsException: 2 >= 1
at java.util.Vector.elementAt(Vector.java:474)
at javax.swing.table.DefaultTableColumnModel.getColumn(DefaultTableColumnModel.java:294)
at org.jdesktop.swingx.JXTable.getColumn(JXTable.java:2265)
at org.jdesktop.swingx.JXTable.getColumnExt(JXTable.java:2415)
at demo.table.AdvancedTableDemo.<init>(AdvancedTableDemo.java:46)
at demo.table.AdvancedTableDemo.run(AdvancedTableDemo.java:62)
at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:311)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:756)
at java.awt.EventQueue.access0(EventQueue.java:97)
at java.awt.EventQueue.run(EventQueue.java:709)
at java.awt.EventQueue.run(EventQueue.java:703)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:726)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:201)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:116)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:105)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:93)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:82)
列数为 3,但大于第 1 列的任何值都会产生刚刚看到的错误!尝试使用 17 列和任何大于 10 returns 的异常。
我是不是遗漏了什么??
The column count is 3, but getting anything greater than column 1 yields the error
int j = baseTable.getColumnCount(true);
for (int i = 0; i < j; i++)
是的,但是隐藏第一列后只有 2 列。然后隐藏下一列后只有一列(因此出现异常)。
所以解决方案是隐藏 table 末尾的列,而不是开头:
int j = baseTable.getColumnCount(true) -1;
for (int i = j; i >= 0; i--)