- 通过内联单元格编辑文本框,组合框或复选框
- 可以同时编辑多个值通过按F2弹出
- 编辑更新基础表数据结构
默认配置设置的单元格编辑defaulteditconfiguration这是用defaultgridlayerconfiguration。默认的配置 告诉nattable不可编辑和设置textcelleditor如 编辑默认编辑器打开特定的单元格。它还增加了验证错误的风格 渲染一个单元格红色的验证错误内容。
在用户交互编辑使几个绑定,需要配置。默认的绑定 配置在defaulteditbindings配置,这也部分的defaultgridlayerconfiguration。
这个editablegridexample和editerrorhandlingexample这两个例子可以看到。
使单元格编辑
编辑了下车默认情况下。启用编辑ieditablerule要 注册。你通常会注册一个ieditablerule对一个单元列的标签 如果你只想为特定条件下,如下面的代码所示启用编辑:
mycelllabel总是编辑标签
你也可以创建你自己的ieditablerule如果你需要决定是否 细胞应可编辑或不。只是确保ieditablerule iseditable()。
创建你自己的ieditablerule很有用,如果你想注册一个ieditablerule在全局内(而不是一个特定的单元格标记), 反应在全局标志(如启用/禁用用户界面中编辑)。
单元格编辑器
这是窗口被激活时,细胞进入编辑模式。一个单元格编辑器需要 实施icelleditor接口。 以下编辑提供的:
- textcelleditor
- checkboxcelleditor
- comboboxcelleditor
- passwordcelleditor(作为一个特殊的textcelleditor)
使用checkboxcelleditor单元格具有标签mycheckboxlabel
而textcelleditor和checkboxcelleditor可以创建没有额外的信息,这comboboxcelleditor需求列表包含在组合框的值。
作为编辑和不同的显示模式渲染分离的任务你应该检查哪些单元格 绘画为你编辑的单元格。有小区的绘画存在的细胞 编辑nattable。
- textcelleditor -魔兽水彩笔
- checkboxcelleditor - checkboxpainter
- comboboxcelleditor - comboboxpainter
- passwordcelleditor - passwordtextpainter
确保使用匹配细胞画家一起编辑
数据转换
nattable呈现每个信息应该显示在细胞内的字符串。支持 其他数据类型的转换是必要的。这是通过注册完成idisplayconverter在电池标签。
使用defaultbooleandisplayconverter细胞具有标签mycheckboxlabel
有几个默认转换器使用nattable:
- defaultdisplayconverter
- defaultcharacterdisplayconverter
- defaultbooleandisplayconverter
- defaultbytedisplayconverter
- defaultshortdisplayconverter
- defaultintegerdisplayconverter
- defaultlongdisplayconverter
- defaultfloatdisplayconverter
- defaultdoubledisplayconverter
- defaultbigintegerdisplayconverter
- defaultbigdecimaldisplayconverter
- defaultdatedisplayconverter
- percentagedisplayconverter
请注意,提供的默认转换器是简单的默认实现。如果你需要一个更 特定的转换,例如将通过双值解析,你会 必须创建自己的。
随时提供任何通用的转换器,适合nattable。
创建你自己的idisplayconverter你应该选择一个现有的 抽象的实现依赖于你的要求:
- displayconverter
扩展这个类来支持简单的价值转换。 - contextualdisplayconverter
扩展这个类来支持价值转换依赖于额外的上下文信息。
数据验证
您可以配置的nattable实例的可编辑单元格的数据有效性验证规则。 模型的更新仅适用于基础列表如果他们通过验证。 添加数据验证的nattable你需要注册idatavalidator在电池标签。
使用数据验证返回的值getsecurtityidvalidator()上犯 细胞具有标签mycelllabel
创建你自己的idatavalidator你应该选择一个现有的 抽象的实现依赖于你的要求:
- datavalidator
扩展这个类来支持简单的数据验证。 - contextualdatavalidator
扩展这个类支持数据验证依赖于额外的上下文信息。
错误处理
转换和验证错误会导致不更新的基础列表的值。 这些错误处理的用户界面可以配置。目前主要有以下 错误处理策略的支持:
- loggingerrorhandling
只会写一个日志项的转换/验证错误消息。 默认如果没有其他错误处理。 - discardvalueerrorhandling
只会关闭打开的编辑器,删除输入的值没有更新 基础列表的值。 - dialogerrorhandling
将显示一个转换/验证错误消息和选择放弃 或改变输入值对话框。弃将关闭编辑器,无需更新值的变化 基础列表,将离开编辑器打开允许改正。 - rendererrorhandling
用于可视化的错误进入到textcelleditor这是默认设置。 使输入的值用红色字体颜色处理转换 和验证。
下面的代码演示如何添加dialogerrorhandling 转换和验证错误。
从editerrorhandlingexample片段
如果你想改变错误而编辑在处理textcelleditor你需要设置你的定制iediterrorhandler作为inputconversionerrorhandler 或inputvalidationerrorhandler。
不允许将这些处理程序空。
文本编辑控件的装饰
如果你想为用户提供一种非侵入性的方式更多的上下文信息,然后您可以使用一个controldecoration 。这允许您显示一个小图标,例如错误或信息/警告图标,对 编辑一边,当鼠标悬停在然后工具提示显示的文本信息。如果你不熟悉的 与controldecorations你几乎肯定已经看到他们在行动在Eclipse作为它的一些内容帮助 信息了!
例如,这里是一个控制装饰集显示在顶部的文本编辑器,编辑器左,用鼠标在图标上显示 因此提示徘徊:
这是非常简单的实现:
从editablegridexample片段
这部作品与细胞icellvalidator串联(和国内的textcelleditors rendererrorhandling实例)。你可以通过调用获得定制进一步的访问textcelleditor getdecorationprovider()。例如力悬停文本能够立即显示而不是在盘旋。返回的controldecorationprovider也 在您自己的自定义编辑重用。
如何把单元格的值
一个单元格编辑器火灾updatedatacommand犯一个小区的新价值。这个命令是由 数据层通过updatedatacommandhandler。 最终idataprovider将更新底层数据结构。
处理不同的数据更新
如果你想执行一个自定义操作,当用户编辑单元格时你会:
- 注销updatedatacommandhandler
- 处理updatedatacommand自定义命令处理程序注册
该代码将看起来像这样