服务热线:13616026886

技术文档 欢迎使用技术文档,我们为你提供从新手到专业开发者的所有资源,你也可以通过它日益精进

位置:首页 > 技术文档 > JAVA > 新手入门 > 基础入门 > 查看文档

关于editor和renderer的一点认识

在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关于editor和renderer的一点认识 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关于editor和renderer的一点认识 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遵守相同的法则

    扫描关注微信公众号