java table中文字颜色,JFace TableViewer - 为表中所有单元格设置字体颜色

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

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值