getTableCellRendererComponent参数的含义 [英] Meaning of getTableCellRendererComponent argument

查看:160
本文介绍了getTableCellRendererComponent参数的含义的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的第一个StackOverflow问题,所以如果我做了一些非常愚蠢的事情,请宽容一点!

This is my very first StackOverflow question, so if I do/ask something incredibly stupid, please be lenient!

对于Java来说,我是一个菜鸟,特别是摇摆,而且我无法理解TableCellRenderer接口的getTableCellRendererComponent函数(和getTableCellEditorComponent函数类似) TableCellEditor接口)。具体来说,我不明白第二个参数(对象类型)的要点。这个值是不是来自给定行和列的给定JTable?如果是这样,为什么还要为第二个参数烦恼呢?我已经做了很多谷歌搜索,但没有人似乎回答这个问题(或者我的大脑有一些故障的神经元,根据过去的历史,这不是一个不太可能的解释......)

I'm a noob when it comes to Java, specifically swing, and I'm having trouble understanding the getTableCellRendererComponent function of the TableCellRenderer interface (and similar with the getTableCellEditorComponent function of the TableCellEditor interface). Specifically, I don't understand the point of the 2nd argument (Object type). Doesn't the value for this just come from the given JTable at the given row and column? If so, why bother with the 2nd argument at all? I've done quite a lot of google searching, but no-one appears to answer this (or perhaps my brain has some malfunctioning neurones, which, given past history, isn't an unlikely explanation...)

我很感激帮助!

谢谢

推荐答案

JTable 渲染是 flyweight pattern ,其中重复移动单个组件以呈现表格单元格的值。每次表确定需要渲染单元格时,表都会调用 getTableCellRendererComponent()。由于它可能被频繁调用,因此应该注意效率。优点是对于不可见的单元格可以省略渲染过程。另请参阅此相关示例

JTable rendering is an example of the flyweight pattern in which a single component is moved repeatedly to render a table cell's value. The getTableCellRendererComponent() is called by the table each time the table determines the need to render a cell. As it may be called frequenly, some care should be given to efficiency. The advantage is that the rendering process can be omitted for non-visible cells. See also this related example.

附录:为什么不直接调用 table.getValueAt(row,column) 而不是使用 value

当它绘制一个单元格时,该表的UI委托,通常是 BasicTableUI 的子类,必须在不知道 TableModel 的情况下调用 prepareRenderer()。表的 prepareRenderer()方法的合约, 知道 TableModel ,指定它通过查询数据模型的值来准备渲染器。有关详细信息,请参见 Swing架构概述

When it paints a cell, the table's UI delegate, typically a subclass of BasicTableUI, must invoke prepareRenderer() without knowledge of the TableModel. The contract of the table's prepareRenderer() method, which does have knowledge of the TableModel, specifies that it "Prepares the renderer by querying the data model for the value." See A Swing Architecture Overview for details.

这篇关于getTableCellRendererComponent参数的含义的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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