本文介绍了TreeViewer颜色行交替的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是新的日食SWT。我试图覆盖 ITableColorProvider 的 getBackground 方法来替代treeViewer的行。我试着用行索引(index%2 == 0)着色。它代替颜色所有的行。



TreeViewer 一次为一个单元格而不是行。任何关于如何实现它的指针(treeviewer的替代行颜色)或代码片段将是非常有用的。

 列表< TreeItem> treeItems = Arrays.asList(m_viewer.getTree()。​​getItems()); 
int index = treeItems.indexOf(element);

if(index%2 == 0)
{
backgroundColor = Display.getDefault()。getSystemColor(
SWT.COLOR_YELLOW);
}
else
{
backgroundColor = Display.getDefault()。getSystemColor(
SWT.COLOR_GRAY);
}


解决方案

ITableColorProvider 用于 TableViewer ,用于 TreeViewer 您的类扩展 LabelProvider 应该实现 IColorProvider

  public MyLabelProvider类扩展LabelProvider实现IColorProvider {

@Override
public String getText(Object element){
//如何为元素
获得标签

@Override
public Color getBackground(Object element){
if(((TreeItem))).getId()%2 == 0){
return显示。 。getCurrent()getSystemColor(SWT.COLOR_BLUE);
} else {
return Display.getCurrent()。getSystemColor(SWT.COLOR_RED);
}
}

@Override
public Color getForeground(Object element){
return null;
}
}

颜色类是来自org.eclipse的颜色类。 swt.graphics.Color。我认为每个TreeItem都有一个id属性,这个属性是顺序生成的。


I am new to eclipse SWT. I am trying to override the getBackground method of ITableColorProvider to color rows alternatively of a treeViewer. I was trying coloring with row index (index%2 == 0). It colors all the rows instead.

TreeViewer colors one cell at a time, instead of rows. Any pointers on how to achieve it (alternate row color for treeviewer) or code snippet will be very helpful.

List<TreeItem> treeItems = Arrays.asList( m_viewer.getTree().getItems() );
int index = treeItems.indexOf( element );

if( index % 2 == 0 )
        {
            backgroundColor = Display.getDefault().getSystemColor(
                    SWT.COLOR_YELLOW );
        }
        else
        {
            backgroundColor = Display.getDefault().getSystemColor(
                    SWT.COLOR_GRAY );
        }
解决方案

ITableColorProvider is used for TableViewer , for TreeViewer your class that extends LabelProvider should implement IColorProvider

public class MyLabelProvider extends LabelProvider implements IColorProvider{

        @Override
        public String getText(Object element) {
            //how the label is obtained for an element
        }

        @Override
        public Color getBackground(Object element) {
            if(((TreeItem) element).getId() % 2 == 0) {
                return Display.getCurrent().getSystemColor(SWT.COLOR_BLUE);
            }else{
                return Display.getCurrent().getSystemColor(SWT.COLOR_RED);
            }                   
        }

        @Override
        public Color getForeground(Object element) {
            return null;
        }
    }

The Color Class is the one from org.eclipse.swt.graphics.Color. I supposed that every TreeItem has an id property that is generated cosecutively

这篇关于TreeViewer颜色行交替的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-17 22:08