【问题标题】:JFace TableViewer - Setting font color for all Cells in tableJFace TableViewer - 为表格中的所有单元格设置字体颜色
【发布时间】:2012-10-01 01:10:08
【问题描述】:

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

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

这是我在 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();
    }
}); 

如何更改上述代码以将字体颜色设置为深灰色?

编辑

如果我使用开关,它如何知道我有多少列? 还有如何设置列名?这是我现在的设置方式

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;
             }
         }
     }
 });

【问题讨论】:

  • 嗯,你知道,你的表有多少列,不是吗? “如何设置列名”是什么意思?也许我的替代解决方案对您来说是更好的选择,因为它涉及的代码更改更少。
  • 我的意思是列标题和列宽。我真的很想学习如何使用 Switch。例如,第 1 列标题应为 itemID,宽度为 100,第 2 列标题应为 revId,宽度为 50
  • 此外,如果您使用第一种替代方法,只需为ColumnLabelProvider 创建一个新类并使用col.setLabelProvider(new YourColumnLabelProvider())。这将减少冗余代码。

标签: java fonts swt jface


【解决方案1】:

我会使用ColumnLabelProvider 的方法update(ViewerCell cell) 而不是getText()。然后就可以拨打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 ...
            }
        }
    }
}

然后使用:

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

由于我switch 是列索引,因此您可以将这个ColorColumnLabelProvider 用于所有列。

别忘了把颜色放在某个地方。

如果您使用ColorConstantsDraw2d,则无需处理它们。

在你的情况下,ColorConstants.darkGray 可以完成这项工作。

替代方案

你也可以定义一个实现IColorProviderColumnLabelProvider

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();
    }

}

【讨论】:

  • 我遇到了 ColorConstants.darkGray 的问题。它要我导入 java.swing.text.StyleConstants。我不能只使用 Swing 的 SWT。 ColorConstants 还能用吗?
  • @jkteater 您必须将 Draw2d jar 添加到您的项目中才能使用ColorConstants。或者通过软件管理器安装 Draw2d。但是,您可以使用 SWT 颜色。但是你必须将它们丢弃在某个地方。
  • 我将 Draw2d 添加到我需要的插件中,它似乎有帮助 - 仍然需要测试
  • 如果我使用系统颜色 - Color dGray = Display.getDefault().getSystemColor(SWT.COLOR_DARK_GRAY);我必须丢弃它吗?
  • @jkteater 不,您不必处理使用此方法获得的颜色。事实上你不能丢弃它。
猜你喜欢
  • 2011-09-03
  • 1970-01-01
  • 2011-10-23
  • 2012-05-24
  • 2017-01-20
  • 2013-06-15
  • 1970-01-01
  • 1970-01-01
  • 2019-10-21
相关资源
最近更新 更多