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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果 JTable 是用 < 构造的a href="http://download.oracle.com/javase/6/docs/api/javax/swing/table/TableModel.html" rel="nofollow noreferrer">TableModel 但没有 TableColumnModel JTable 将创建一个 TableColumnModel 使用 createDefaultColumnModel() 并设置 autoCreateColumnsFromModel 为 true。当此属性为 true 时,JTable将使用值填充 TableColumnModel来自 TableModel。
似乎没有人能保证两者保持同步。举个例子,JTable。 getColumnName() 将返回 TableModel 列名称,无论 TableColumnModel 实际上显示在屏幕上。
我注意到的另一个有趣的事情是 TableModel 仅限于字符串列,而 TableColumnModel< /a> 允许您将任何对象传递到 TableCell渲染器。 Javadoc 说这些值仅限于字符串,但实际上这是特定于实现的。没有什么可以阻止您编写使用 JComponent< 的实现/a> 值。
总结: TableColumnModel 是列值的最终所有者。 TableColumnModel 仅询问TableModel 仅当值不存在时已经有一个了。例如,在将列传递到 JTable.addColumn() 而不指定标题值。
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.
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.
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.