Changing the Background Color
A
useful
technique that enables you to visually delimit data in a table is to
change the background color of a TableItem
.
How do I do that?
Use setBackground()
to
specify the background
color of an individual
TableItem
:
item2.setBackground(new Color(d,127,178,127));
Adding this code to the Example 12-1 class results in Figure 12-6.
Figure 12-6. Setting the background color
What just happened?
setBackground( )
accepts a
Color
object created by passing a reference to the
Display
in which the table is being managed, as
well as three int
values representing the
Red
, Green
, and
Blue
values of the desired color.
This technique is useful in creating alternating “bar paper” looks that visually separate rows of data (the Quicken check register program is an example that uses this technique).
What about...
the times
you want to cause a row to be
highlighted if a cell in that row contains
a particular value? Using the table from
TableShellExample
as a basis, the following code
will cause all rows in which the Address column contains the word
“Ohio” to be highlighted by
changing the background color to green:
TableItem[] tia = t.getItems( ); for(int i = 0; i<tia.length;i++) { if (tia[i].getText(2).equals("Ohio")) { tia[i].setBackground(new Color(d,127,178,127)); } }
The preceding code retrieves an array of items from the table and then loops through the array. For every ...
Get SWT: A Developer's Notebook now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.