关于Editor和Renderer的一点认识

王朝java/jsp·作者佚名  2006-01-09
宽屏版  字体: |||超大  

在Sun的官方网站上对Editor和Renderer的解释是如下:

Renderer:

Instead, a single cell renderer is generally used to draw all of the cells that contain the same type of data. You can think of the renderer as a configurable ink stamp that the table uses to stamp apPRopriately formatted data onto each cell. When the user starts to edit a cell's data, a cell editor takes over the cell, controlling the cell's editing behavior.

个人理解就是对于不同的数据显示不同的格式,相当于在MVC中根据不同的Model选择不同的View,Renderer就是给你这个选择的权利。默认的几种数据的显示方式如下:

Boolean — rendered with a check box.

Number — rendered by a right-aligned label.

Double, Float — same as Number, but the object-to-text translation is performed by a NumberFormat

instance (using the default number format for the current locale).

Date — rendered by a label, with the object-to-text translation performed by a DateFormat

instance (using a short style for the date and time).

ImageIcon, Icon — rendered by a centered label.

Object — rendered by a label that displays the object's string value.

假如你有非凡的数据,想有非凡的显示方式,可以自己设定Cell的Renderer如下:

TableCellRenderer weirdRenderer = new WeirdRenderer();

table = new JTable(...) {

public TableCellRenderer getCellRenderer(int row, int column) {

if ((row == 0) && (column == 0)) {

return weirdRenderer;

}

// else...

return super.getCellRenderer(row, column);

}

};

Editor是用于编辑数据,但是Renderer是用于显示数据。

关于Editor,和Renderer遵守相同的法则

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
© 2005- 王朝网络 版权所有