设置不可见后无法访问Jtable列数据 [英] Cannot access the Jtable column data after set invisible

查看:192
本文介绍了设置不可见后无法访问Jtable列数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

将JTable列设置为不可见后,我无法获得对该组件的引用。

After setting a JTable column to invisible, i cannot get reference to the component.

Component cellEditor;
String name;
String value;
// loop row then column
for(int j=0 ; j<tolCol ; j++){
    /* get cell name in JTable */
    System.out.println(j + " " + ((JTable)comp).getModel().getValueAt(0, j));
    tce = ((JTable)comp).getCellEditor(0, j); // <-- ArrayIndexOutOfBounds
}

当我使用System.out.println时( j ++((JTable)comp)。getModel()。getValueAt(0,j));我设法打印出所有的价值。还有什么我需要做的才能获得组件的引用吗?

When i use System.out.println(j + " " + ((JTable)comp).getModel().getValueAt(0, j)); i manage to print all the value out. Is there anything else i need to do to get the reference of component?

推荐答案

为什么你认为你需要单元格的编辑器?一个隐藏的列。这听起来像是一个糟糕的设计。

Why do you think you need the cell editor of a hidden column. It sounds like a bad design.

即使您访问数据的代码也不正确,因为模型和视图可能不同。模型中列的顺序永远不会改变。让我们假设您的表模型可以包含3列中的以下数据:String,I​​nteger,Date。现在让我们假设用户通过将Date列拖到表的第一列来重新排序表。

Even your code to access the data is not corrent because the model and the view can be different. The order of the columns in the model never change. Lets assume your table model can contain the following data in 3 columns: "String", "Integer", "Date". Now lets assume the user reorders the table by dragging the "Date" columns to the first column of the table.

现在访问table.getModel()。getValueAt( row,0)你将得到一个字符串值。

Now when you access table.getModel().getValueAt(row, 0) you will get a "String" value.

当你访问table.getCellEditor(row,0)时,你将得到一个Date对象的编辑器。

When you access table.getCellEditor(row, 0) you will get the editor for a "Date" object.

要访问给定类类型的默认编辑器,您可以执行以下操作:

To access the default editor for a given Class type you can do:

Class clazz = table.getModel().getColumnClass(0);
TableCellEditor tce = table.getDefaultEditor( clazz );

这篇关于设置不可见后无法访问Jtable列数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆