在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遵守相同的法则
闽公网安备 35060202000074号