如何修改NatTable选择着色使单元格颜色透出及BlendedBackgroundPainter用法
NatTable选择背景色混合解决方案
BlendedBackgroundPainter完全符合你的需求,它就是NatTable专门用来实现「单元格自定义背景色 + 选中状态背景色」混合叠加的组件,和SWT Table默认的颜色混合效果逻辑一致。
使用步骤
- 用
BlendedBackgroundPainter作为装饰器,包裹你已有的自定义单元格背景Painter,它会自动在自定义背景之上,根据选中状态叠加半透明的选择背景色:
// 你的自定义单元格背景Painter ICellPainter customBgPainter = new YourCustomCellBackgroundPainter(); // 用BlendedBackgroundPainter包裹自定义Painter BlendedBackgroundPainter blendedPainter = new BlendedBackgroundPainter(customBgPainter); // 将混合Painter绑定到单元格样式配置 yourCellStyleConfiguration.setCellPainter(blendedPainter);
- 自定义混合参数(可选):
- 调整混合透明度:默认是50%透明度,可通过构造方法指定百分比(0-100)
// 设置70%透明度的混合效果 BlendedBackgroundPainter blendedPainter = new BlendedBackgroundPainter(customBgPainter, 70); - 自定义混合颜色:替换默认的选择背景色
Color customBlendColor = new Color(Display.getCurrent(), 180, 210, 255); blendedPainter.setBlendColor(customBlendColor);
- 调整混合透明度:默认是50%透明度,可通过构造方法指定百分比(0-100)
注意事项
- 确保你的自定义Painter负责绘制单元格的基础背景,
BlendedBackgroundPainter会在其绘制完成后处理选中状态的混合叠加,无需修改原有自定义Painter的逻辑。 - 如果你的单元格样式中同时设置了
backgroundColor,BlendedBackgroundPainter也会将该颜色纳入混合计算,适配各种自定义背景场景。
内容的提问来源于stack exchange,提问作者Chris Lewold
相关产品推荐
相关产品推荐

