本文介绍了TableModel与ColumnModel:谁拥有列值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

JTable.getModel()。getColumnName() JTable.getColumnModel()。getColumn(index).getHeaderValue()之间的区别是什么? ?这两个似乎没有共享任何数据。我的猜测是 TableModel.getColumnName()表示列的文本表示,而 TableColumn.getHeaderValue() TableColumn.getHeaderRenderer()确定列的外观(它不需要是纯文本)。

What's the difference between JTable.getModel().getColumnName() and JTable.getColumnModel().getColumn(index).getHeaderValue()? The two don't seem to share any data. My guess is that TableModel.getColumnName() indicates the textual representation of a column while TableColumn.getHeaderValue() and TableColumn.getHeaderRenderer() determine what the column looks like (it doesn't need to be plain text).

什么保证两者保持同步?如果两者冲突会发生什么?

What guarantees that the two are kept in sync? What happens if the two conflict?

推荐答案

如果由但没有 将创建一个使用并设置为true。当此属性为true时,将使用值填充来自。

If a JTable is constructed with a TableModel but without a TableColumnModel the JTable will create a TableColumnModel using createDefaultColumnModel() and set autoCreateColumnsFromModel to true. When this property is true, the JTable will populate the TableColumnModel with values from the TableModel.

似乎没有人保证两者保持同步。例如,将返回列名,无论实际显示在屏幕上。

No one seems to guarantee that the two are kept in sync. Case in point, JTable.getColumnName() will return the TableModel column name regardless of what the TableColumnModel actually displays on the screen.

我注意到的另一个有趣的事情是仅限于String列,而允许您将任何Object传递给。 Javadoc 这些值仅限于字符串,但实际上这是特定于实现的。没有什么能阻止您编写使用是列值的最终所有者。 仅询问仅在值不是已经有一个。例如,在您将列传递到,没有指定标题值。

In summary: TableColumnModel is the ultimate owner of column values. TableColumnModel only asks TableModel for values only if it doesn't already have one. For example, in the case where you pass a column into JTable.addColumn() without specifying a header value.

这篇关于TableModel与ColumnModel:谁拥有列值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-15 03:27