我试图将隐藏列的数据显示为工具提示.使用以下代码隐藏正常工作:
JTable table = new JTable(model){
//Implement table cell tool tips.
public String getToolTipText(MouseEvent e) {
String tip = null;
java.awt.Point p = e.getPoint();
int rowIndex = rowAtPoint(p);
int colIndex = columnAtPoint(p);
int realColumnIndex = convertColumnIndexToModel(colIndex);
try {
tip = getValueAt(rowIndex, 8).toString();
} catch (RuntimeException e1) {
//catch null pointer exception if mouse is over an empty line
}
return tip;
}
};
TableColumnModel tcm = table.getColumnModel();
TableColumn tc;
for(int i = 1; i <= 7; i++){
tc = tcm.getColumn(8);
tcm.removeColumn(tc);
}
Run Code Online (Sandbox Code Playgroud)
但是工具提示没有显示隐藏列的数据(getValue函数没有返回值).那么隐藏列也会删除数据吗?
i变量;-)如removeColumn在JTablejavadoc中明确说明的那样,on 不会从模型中删除数据
从此JTable的列数组中删除aColumn.注意:此方法不会从模型中删除数据列; 它只是删除了负责显示它的TableColumn.
有一个在Javadoc来同样的方法没有提及TableColumnModel,但我认为它的工作方式相同,但你总是可以给它一个尝试调用它的JTable,而不是
代码中的真正问题是使用getValueAt,它使用表的行和列索引,而不是模型
注意:该列在表视图的显示顺序中指定,而不是在TableModel的列顺序中指定.这是一个重要的区别,因为当用户重新排列表中的列时,视图中给定索引处的列将发生更改.同时,用户的操作永远不会影响模型的列排序.