Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » Standard Widget Toolkit (SWT) » jface alternating row background color
jface alternating row background color [message #511346] Mon, 01 February 2010 12:03 Go to next message
Luca Ferrari is currently offline Luca FerrariFriend
Messages: 159
Registered: November 2009
Senior Member
Hi all,
I'd like to customize the alternating of background colors in a tableviewer, but getBackground of IColorProvider is called for each row cell, so the only way I found to do it is with the following code in my label provider:

public Color getBackground(Object element) {
	// select the right color
	Color colorToUseForThisRow = DEFAULT_BACKGROUND_NORMAL_COLOR;
	
	if( element == null )
	    return colorToUseForThisRow;
	
	
	// check if this element has a different hash code of the previous one
	if( this.lastElementHashCode != element.hashCode() ){
	    this.lastElementHashCode = element.hashCode();
	    this.rowNumber++;
	}
	

	// decide which color to use for every row
	if( this.rowNumber % 2 == 0 )
	    colorToUseForThisRow = DEFAULT_BACKGROUND_ODD_ROW_COLOR;
	else
	    colorToUseForThisRow = DEFAULT_BACKGROUND_EVEN_ROW_COLOR;
	
	// all done
	return colorToUseForThisRow;
    }



Is there a smarter way to achieve the same result? It will be nice to have an interface with a method called for each table row.

Thanks.
Re: jface alternating row background color [message #511467 is a reply to message #511346] Mon, 01 February 2010 11:36 Go to previous messageGo to next message
Daniel Krügler is currently offline Daniel KrüglerFriend
Messages: 853
Registered: July 2009
Senior Member
On 01.02.2010 13:03, Luca Ferrari wrote:
> Hi all,
> I'd like to customize the alternating of background colors in a
> tableviewer, but getBackground of IColorProvider is called for each row
> cell, so the only way I found to do it is with the following code in my
> label provider:
>
>
> public Color getBackground(Object element) {
> // select the right color
> Color colorToUseForThisRow = DEFAULT_BACKGROUND_NORMAL_COLOR;
>
> if( element == null )
> return colorToUseForThisRow;
>
>
> // check if this element has a different hash code of the previous one
> if( this.lastElementHashCode != element.hashCode() ){
> this.lastElementHashCode = element.hashCode();
> this.rowNumber++;
> }
>
>
> // decide which color to use for every row
> if( this.rowNumber % 2 == 0 )
> colorToUseForThisRow = DEFAULT_BACKGROUND_ODD_ROW_COLOR;
> else
> colorToUseForThisRow = DEFAULT_BACKGROUND_EVEN_ROW_COLOR;
>
> // all done
> return colorToUseForThisRow;
> }
>
>
>
> Is there a smarter way to achieve the same result? It will be nice to
> have an interface with a method called for each table row.

Yes. I recommend to use the new Eclipse 3.3 viewer API. This means
that you register different CellLabelProvider per table column.
Formatting of the cell happens inside the

void update(ViewerCell cell);

method. From the ViewerCell you can ask for the current TableItem
(assuming you have a TableViewer) as follows:

TableItem ti = (TableItem) cell.getItem();
final int rowIndex = ti.getParent().indexOf(ti);

as you see you can easily ask for the current row-index (zero-
based).

HTH & Greetings from Bremen,

Daniel Krügler
Re: jface alternating row background color [message #511521 is a reply to message #511467] Mon, 01 February 2010 21:08 Go to previous messageGo to next message
Luca Ferrari is currently offline Luca FerrariFriend
Messages: 159
Registered: November 2009
Senior Member
Thanks for the reply, but I don't understand how to convert an ITableLabelProvider to a set of ColumnLabelProvider. I mean, in the ColumnLabelProvider is quite easy to convert a business object to its column representation:

public String getColumnText(Object element, int col){
   switch(col){
     case 0: // first column text
     case 1: //second column of text
    ...
   }
}


but in the case of ColumnLabelProvider I've got only a getText(Object element) method, so it sounds to me as I have to create a class for each column of each table. I'm sure there is a smart way to convert the table label provider to a set of column label provider, anybody can give me an hint?

Thanks.
Re: jface alternating row background color [message #511581 is a reply to message #511521] Tue, 02 February 2010 07:20 Go to previous messageGo to next message
Daniel Krügler is currently offline Daniel KrüglerFriend
Messages: 853
Registered: July 2009
Senior Member
On 01.02.2010 22:08, Luca Ferrari wrote:
> Thanks for the reply, but I don't understand how to convert an
> ITableLabelProvider to a set of ColumnLabelProvider. I mean, in the
> ColumnLabelProvider is quite easy to convert a business object to its
> column representation:
>
>
> public String getColumnText(Object element, int col){
> switch(col){
> case 0: // first column text
> case 1: //second column of text
> ...
> }
> }
>
>
> but in the case of ColumnLabelProvider I've got only a getText(Object
> element) method, so it sounds to me as I have to create a class for each
> column of each table. I'm sure there is a smart way to convert the table
> label provider to a set of column label provider, anybody can give me an
> hint?

Please check out

http://wiki.eclipse.org/index.php/JFaceSnippets

for some examples regarding the new 3.3 API (look for 3.3 in the text).

The original articles provided by Tom Schindl may also help:

http://tom-eclipse-dev.blogspot.com/2006/12/new-faces-of-jfa ce-part-1.html
http://tom-eclipse-dev.blogspot.com/2007/01/new-faces-of-jfa ce-part2.html

The approach of CellLabelProviders and EditingSupport is different
from the single label provider and single editor: Instead of a need to
implement getText etc. you implement the

void update(ViewerCell cell)

method and you set everything you need to set to the cell, e.g.

cell.setText("My text");
cell.setBackground(...);

HTH & Greetings from Bremen,

Daniel Krügler
Re: jface alternating row background color [message #511604 is a reply to message #511581] Tue, 02 February 2010 08:54 Go to previous messageGo to next message
Thomas Schindl is currently offline Thomas SchindlFriend
Messages: 6651
Registered: July 2009
Senior Member
[...]

> Please check out
>
> http://wiki.eclipse.org/index.php/JFaceSnippets
>
> for some examples regarding the new 3.3 API (look for 3.3 in the text).
>
> The original articles provided by Tom Schindl may also help:
>
> http://tom-eclipse-dev.blogspot.com/2006/12/new-faces-of-jfa ce-part-1.html
> http://tom-eclipse-dev.blogspot.com/2007/01/new-faces-of-jfa ce-part2.html
>

http://tomsondev.bestsolution.at/2006/12/06/the-new-faces-of -jface-part-1/
http://tomsondev.bestsolution.at/2007/01/18/the-new-faces-of -jface-part2/

Are the new ones - with a much nicer desgin :-)

Tom
Re: jface alternating row background color [message #511605 is a reply to message #511604] Tue, 02 February 2010 04:02 Go to previous messageGo to next message
Thomas Schindl is currently offline Thomas SchindlFriend
Messages: 6651
Registered: July 2009
Senior Member
http://wiki.eclipse.org/JFaceSnippets#Snippet041TableViewerA lternatingColors

Tom

Am 02.02.10 09:54, schrieb Tom Schindl:
> [...]
>
>> Please check out
>>
>> http://wiki.eclipse.org/index.php/JFaceSnippets
>>
>> for some examples regarding the new 3.3 API (look for 3.3 in the text).
>>
>> The original articles provided by Tom Schindl may also help:
>>
>> http://tom-eclipse-dev.blogspot.com/2006/12/new-faces-of-jfa ce-part-1.html
>> http://tom-eclipse-dev.blogspot.com/2007/01/new-faces-of-jfa ce-part2.html
>>
>
> http://tomsondev.bestsolution.at/2006/12/06/the-new-faces-of -jface-part-1/
> http://tomsondev.bestsolution.at/2007/01/18/the-new-faces-of -jface-part2/
>
> Are the new ones - with a much nicer desgin :-)
>
> Tom
Re: jface alternating row background color [message #512032 is a reply to message #511605] Wed, 03 February 2010 17:12 Go to previous message
Daniel Krügler is currently offline Daniel KrüglerFriend
Messages: 853
Registered: July 2009
Senior Member
On 02.02.2010 09:59, Tom Schindl wrote:
> http://wiki.eclipse.org/JFaceSnippets#Snippet041TableViewerA lternatingColors

Well, I considered to provide this link but according to my own
experience the proposed solution there is both complicated and
less efficient compared to the rather simple evaluation of the
underlying widget's index value. Have you compared both versions
for in current SWT versions?

Greetings from Bremen,

Daniel Krügler
Previous Topic:SWT Multiline text resizing and wrapping
Next Topic:How to implement ExpandBar Like check sheet or Dynamic Help
Goto Forum:
  


Current Time: Fri Apr 19 22:58:34 GMT 2024

Powered by FUDForum. Page generated in 0.03235 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top