java rcp中lable设置透明_rcp tableViewer 每个单元格都添加图片装饰

Eclipse does not support decorating TableViewers and TreeViewers with TreeColumns as they have proposed Decorators for TreeViewers. But in many cases where we use TreeViewers with TreeColumns or TableViewers we need to provide some decorations based on some Condition.

This can be achieved by extending the DecoratingLabelProvider class and creating your own class called as for e.g.TableDecoratingLabelProvider and implement ITableLabelProvider in that class.

It would look something like this

/**

* Class that supports Decoration of TableViewer and TreeViewer with TreeColumns

*/

package decorators;

import org.eclipse.jface.viewers.DecoratingLabelProvider;

import org.eclipse.jface.viewers.ILabelDecorator;

import org.eclipse.jface.viewers.ILabelProvider;

import org.eclipse.jface.viewers.ITableLabelProvider;

import org.eclipse.swt.graphics.Image;

/**

*

*/

public class TableDecoratingLabelProvider extends DecoratingLabelProvider

implements ITableLabelProvider {

ITableLabelProvider provider;

ILabelDecorator decorator;

/**

* @param provider

* @param decorator

*/

public TableDecoratingLabelProvider(ILabelProvider provider,

ILabelDecorator decorator) {

super(provider, decorator);

this.provider = (ITableLabelProvider) provider;

this.decorator = decorator;

}

/* (non-Javadoc)

* @see org.eclipse.jface.viewers.ITableLabelProvider#getColumnImage(java.lang.Object, int)

*/

public Image getColumnImage(Object element, int columnIndex) {

Image image = provider.getColumnImage(element, columnIndex);

if (decorator != null) {

Image decorated = decorator.decorateImage(image, element);

if (decorated != null) {

return decorated;

}

}

return image;

}

/* (non-Javadoc)

* @see org.eclipse.jface.viewers.ITableLabelProvider#getColumnText(java.lang.Object, int)

*/

public String getColumnText(Object element, int columnIndex) {

String text = provider.getColumnText(element, columnIndex);

if (decorator != null) {

String decorated = decorator.decorateText(text, element);

if (decorated != null) {

return decorated;

}

}

return text;

}

This class can now handle Trees which have Columns and TableViewers. How you use it is specified below

While attaching the LabelProvider to your viewer use this Code

ILabelDecorator decorator = PlatformUI.getWorkbench().getDecoratorManager().getLabelDecorator();

.setLabelProvider(new TableDecoratingLabelProvider(, decorator));

instead of

ILabelDecorator decorator = PlatformUI.getWorkbench().getDecoratorManager().getLabelDecorator();

.setLabelProvider(new DecoratingLabelProvider(, decorator));

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

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
©️2022 CSDN 皮肤主题:游动-白 设计师:我叫白小胖 返回首页

打赏作者

weixin_39548776

你的鼓励将是我创作的最大动力

¥2 ¥4 ¥6 ¥10 ¥20
输入1-500的整数
余额支付 (余额:-- )
扫码支付
扫码支付:¥2
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值