关于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 (in the API reference documentation) 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 (in the API reference documentation) 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遵守相同的法则

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值