简体   繁体   中英

Error in changing background color of row in JFace tableviewer

I want to change the row color of the tableviewer depends on certain conditions. I have used the following code snippet.

tableViewer.setLabelProvider(new ColumnLabelProvider(){
        @Override
        public Color getForeground(Object element){
             super.clearListeners();
             if((((MyClass)element).getMyMethodValue()).equals("ABC"))
               return Display.getDefault().getSystemColor(SWT.COLOR_GRAY);
             else
               return null;
        }
    });

The color of the row is changing but the text each cell is changed to the object name of model class "MYClass" like com.sample.JfaceProject.model.MyClass@394083 . I am cofused why the text is changed in every cell of the table. Am I doing it correctly? Can anyone suggest me some ideas.

Thanks in Advance

If you use a ColumnLabelProvider you must use it for everything to do with the column - so you need to override getText as well as getForeground .

The default getText for ColumnLabelProvider is

public String getText(Object element) {
  return element == null ? "" : element.toString();//$NON-NLS-1$
}

It is the element.toString() which is producing the 'com.sample.JfaceProject.model.MyClass@394083' output - this is the default Object.toString() output.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM