TableModel与ColumnModel:谁拥有列值? [英] TableModel vs ColumnModel: who owns the column value?

查看:168
本文介绍了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?

推荐答案

如果 JTable TableModel 但没有 TableColumnModel JTable 将创建一个 TableColumnModel 使用 createDefaultColumnModel()并设置 autoCreateColumnsFromModel 为true。当此属性为true时, JTable 将使用值填充 TableColumnModel 来自 TableModel

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.

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

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.

我注意到的另一个有趣的事情是 TableModel 仅限于String列,而 TableColumnModel 允许您将任何Object传递给 TableCellRenderer 。 Javadoc 这些值仅限于字符串,但实际上这是特定于实现的。没有什么能阻止您编写使用 JComponent <的实现/ a>值。

Another interesting thing I noticed is that TableModel is limited to String columns whereas TableColumnModel allows you to pass any Object to the TableCellRenderer. The Javadoc says that the values are restricted to Strings but in fact this is implementation-specific. Nothing prevents you from writing an implementation that uses a JComponent value.

总结 TableColumnModel 是列值的最终所有者。 TableColumnModel 仅询问 TableModel 仅在值不是已经有一个。例如,在您将列传递到 JTable.addColumn(),没有指定标题值。

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:谁拥有列值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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