JFace TableViewer - 为表中所有单元格设置字体颜色 [英] JFace TableViewer - Setting font color for all Cells in table

查看:1398
本文介绍了JFace TableViewer - 为表中所有单元格设置字体颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我问了一个非常类似的问题,但我最终使用图像,而不是改变颜色。

我希望单元格中的所有文本都是深灰色的。我知道你必须分配每一列。但我不知道该怎么做。



这是我的TableViewer中的一个列。

  col =新的TableViewerColumn(this,SWT.NONE); 
col.getColumn()。setWidth(200);
col.getColumn()。setText(Printer / Profile);
col.setLabelProvider(new ColumnLabelProvider(){
@Override
public String getText(Object element){
AplotResultsDataModel.ResultsData p =(AplotResultsDataModel.ResultsData)element;
return p.getPrinterProfile();
}
});

如何更改上面的代码,将字体颜色设置为深灰色?



编辑

如果我使用开关,它怎么知道我有多少列?
也如何设置列名?这里是我现在如何设置它

  TableViewerColumn col = new TableViewerColumn(this,SWT.NONE); 
col.getColumn()。setWidth(150);
col.getColumn()。setText(ItemId);
col.setLabelProvider(new ColumnLabelProvider(){
@Override
public void update(ViewerCell cell)
{
Object element = cell.getElement();
if(元素instanceof AplotPDFDataModel.FileNameData)
{
AplotPDFDataModel.FileNameData p =(AplotPDFDataModel.FileNameData)元素;
cell.setForeground(ColorConstants.darkGray);
switch( cell.getColumnIndex())
{
case 0:
try {
cell.setText(p.getRev()。getStringProperty(item_id));
}
catch(TCException e){
e.printStackTrace();
}
break;
case 1:
try {
cell .setText(p.getRev()。getStringProperty(item_revision_id));
}
cat ch(TCException e){
e.printStackTrace();
}
break;
案例2:
cell.setText(p.getPRLValue()。toString());
break;
案例3:
cell.setText(p.getMarkupValue());
break;
案例4:
cell.setText(p.getFileName());
break;
}
}
}
});


解决方案

我会使用 ColumnLabelProvider 代替<%c $ c>更新(ViewerCell单元格) code>的getText()。然后你可以调用 ViewerCell#setForeground(Color color)

<$ (ColorColumnLabelProvider)扩展ColumnLabelProvider {
@Override
public void update(ViewerCell cell)
{
Object element = cell.getElement() ;
if(element instanceof AplotResultsDataModel.ResultsData)
{
AplotResultsDataModel.ResultsData p =(AplotResultsDataModel.ResultsData)element;

cell.setForeground(YOUR_COLOR);
switch(cell.getColumnIndex())
{
case 0:
cell.setText(p.YOUR_FIRST_TEXT);
break;
案例1:
cell.setText(p.YOUR_SECOND_TEXT);
break;
case ...








然后使用:

  col.getColumn()。setWidth(150); 
col.getColumn()。setText(ItemId);
col.setLabelProvider(new ColorColumnLabelProvider());

由于我 switch 列索引,可以使用这个 ColorColumnLabelProvider 作为所有的列。



不要忘记在某处放置颜色。



如果您使用 ColorConstants Draw2d ,你不需要把它们处理掉。



ColorConstants.darkGray 可以完成这项工作。
$ b 其他

你也可以定义一个 ColumnLabelProvider 来实现 IColorProvider : / p>

  public class ColorColumnLabelProvider extends ColumnLabelProvider implements IColorProvider {
$ b @Override
public Color getBackground(Object元素){
返回null;
}

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

@Override
public String getText(Object element){
AplotResultsDataModel.ResultsData p =(AplotResultsDataModel.ResultsData)element;
return p.getPrinterProfile();
}

}


I have asked a very similar question, but I ended up using images instead of changing the color.

I want all the text in the cells to be dark grey. I understand that you have to assign each column. But I do not how to do it.

This is one of my columns in my TableViewer.

col = new TableViewerColumn(this , SWT.NONE);
col.getColumn().setWidth(200);
col.getColumn().setText("Printer/Profile");
col.setLabelProvider(new ColumnLabelProvider() {
    @Override
    public String getText(Object element) {
        AplotResultsDataModel.ResultsData p = (AplotResultsDataModel.ResultsData) element;
        return p.getPrinterProfile();
    }
}); 

How would I change the above code to incorporate setting the font color to dark gray?

EDIT

If I am using the switch, how does it know how many columns I have? also how do I set the column names? Here is how I have it set up right now

TableViewerColumn col = new TableViewerColumn(this , SWT.NONE);
  col.getColumn().setWidth(150);
  col.getColumn().setText("ItemId");
  col.setLabelProvider(new ColumnLabelProvider() {
     @Override
     public void update(ViewerCell cell)
     {
         Object element = cell.getElement();
         if(element instanceof AplotPDFDataModel.FileNameData)
         {
            AplotPDFDataModel.FileNameData p = (AplotPDFDataModel.FileNameData) element;
            cell.setForeground(ColorConstants.darkGray);
            switch(cell.getColumnIndex())
            {
               case 0:
                  try {
                     cell.setText(p.getRev().getStringProperty("item_id"));
                  }
                  catch (TCException e) {
                     e.printStackTrace();
                  }
                  break;
               case 1:
                  try {
                     cell.setText(p.getRev().getStringProperty("item_revision_id"));
                  }
                  catch (TCException e) {
                     e.printStackTrace();
                  }
                  break;
               case 2:
                  cell.setText(p.getPRLValue().toString());
                  break;
               case 3:
                  cell.setText(p.getMarkupValue());
                  break;
               case 4:
                  cell.setText(p.getFileName());
                  break;
             }
         }
     }
 });

解决方案

I would use the method update(ViewerCell cell) of the ColumnLabelProvider instead of getText(). Then you can call ViewerCell#setForeground(Color color):

public class ColorColumnLabelProvider extends ColumnLabelProvider {
    @Override
    public void update(ViewerCell cell)
    {
        Object element = cell.getElement();
        if(element instanceof AplotResultsDataModel.ResultsData)
        {
            AplotResultsDataModel.ResultsData p = (AplotResultsDataModel.ResultsData) element;

            cell.setForeground(YOUR_COLOR);
            switch(cell.getColumnIndex())
            {
                case 0:
                    cell.setText(p.YOUR_FIRST_TEXT);
                    break;
                case 1:
                    cell.setText(p.YOUR_SECOND_TEXT);
                    break;
                case ...
            }
        }
    }
}

Then use:

col.getColumn().setWidth(150);
col.getColumn().setText("ItemId");
col.setLabelProvider(new ColorColumnLabelProvider());

Since I switch the column index, you can use this ColorColumnLabelProvider for all your columns.

Don't forget to dispose the color somewhere.

If you use ColorConstants of Draw2d, you don't need to dispose them.

In your case ColorConstants.darkGray would do the job.

ALTERNATIVE:

You can also define a ColumnLabelProvider that implements IColorProvider:

public class ColorColumnLabelProvider extends ColumnLabelProvider implements IColorProvider {

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

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

    @Override
    public String getText(Object element) {
        AplotResultsDataModel.ResultsData p = (AplotResultsDataModel.ResultsData) element;
        return p.getPrinterProfile();
    }

}

这篇关于JFace TableViewer - 为表中所有单元格设置字体颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆