Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » Standard Widget Toolkit (SWT) » possible bug in TableItem.getBackground(index)
possible bug in TableItem.getBackground(index) [message #444652] Tue, 19 October 2004 13:14 Go to next message
user is currently offline userFriend
Messages: 296
Registered: July 2009
Senior Member
Hi,

After setting the background color of a table cell
TableItem.getBackground(index) doesnt reflect the changes, it always
returns the old value.
I've added an example with a demo of the problem and a workaround
(commented out). Click on a cell and it should switch between green and
red for each klick.

Can anyone confirm this problem ?


thanks in advance,
Yves Harms


package org.eclipse.swt.snippets;

import org.eclipse.jface.viewers.*;
import org.eclipse.swt.*;
import org.eclipse.swt.graphics.*;
import org.eclipse.swt.layout.*;
import org.eclipse.swt.widgets.*;

public class Snippet129 {

public void createPartControl(Composite parent){
final Color red = parent.getDisplay().getSystemColor(SWT.COLOR_RED);
final Color green = parent.getDisplay().getSystemColor(SWT.COLOR_GREEN);
final Table table = new Table(parent, SWT.BORDER);
//table.setBackground(yellow);
table.setHeaderVisible(true);

String[] columnNames = {"a","b","c"};
TableColumn col1 = new TableColumn(table, SWT.NONE);
TableColumn col2 = new TableColumn(table, SWT.NONE);
TableColumn col3 = new TableColumn(table, SWT.NONE);
col1.setText(columnNames[0]);
col2.setText(columnNames[1]);
col3.setText(columnNames[2]);
col1.pack();
col2.pack();
col3.pack();


TableItem item = new TableItem(table, SWT.NONE);
item.setText(new String[] {"white fore/blue back","normal","white
fore/red back"});
item.setBackground(0, green);
item.setBackground(2, red);

item = new TableItem(table, SWT.NONE);
item.setText(new String[] {"white fore/blue back","normal","white
fore/red back"});
item.setBackground(0, green);
item.setBackground(2, red);

table.addListener (SWT.MouseDown, new Listener () {
public void handleEvent (Event event) {
Rectangle clientArea = table.getClientArea ();
Point pt = new Point (event.x, event.y);
int index = table.getTopIndex ();
while (index < table.getItemCount ()) {
boolean visible = false;
TableItem item = table.getItem (index);
for (int i=0; i < table.getColumnCount(); i++) {
Rectangle rect = item.getBounds (i);
if (rect.contains (pt)) {
TableItem row = table.getItem(index);
System.out.println (row.getBackground(i));


if(row.getBackground(i)==red){
row.setBackground(i, green);
}else{
row.setBackground(i, red);
}

/*

//WORKAROUND: store colors in data objects
Object data = row.getData("bgColor");
Color[] cellColors;
if(data==null){
cellColors = new Color[table.getColumnCount()];
}else{
System.out.println(data.getClass());
cellColors = (Color[])data;

}
//if(cellColors[i]==null||cellColors[i]==red){
if(row.getBackground(i)==red){
row.setBackground(i, green);
cellColors[i]=green;

row.setData("bgColor",cellColors);
}else{
row.setBackground(i, red);
cellColors[i]=red;
row.setData("bgColor",cellColors);
}

*/

table.redraw();

}
if (!visible && rect.intersects (clientArea)) {
visible = true;
}
}
if (!visible) return;
index++;
}
}
});


}


public static void main(String[] args) {
Display display = new Display();
Snippet129 app = new Snippet129();

Shell shell = new Shell(display);
shell.setLayout(new FillLayout());
app.createPartControl(shell);

shell.pack();
shell.open();

while (!shell.isDisposed()) {
if (!display.readAndDispatch())
display.sleep();
}
display.dispose();
}
}
Re: possible bug in TableItem.getBackground(index) [message #444654 is a reply to message #444652] Tue, 19 October 2004 13:57 Go to previous message
Veronika Irvine is currently offline Veronika IrvineFriend
Messages: 1272
Registered: July 2009
Senior Member
Instead of using:

if (row.getBackground(i) == red){

you should be using:

if (row.getBackground(i).equals(red)){

The Colour object you pass in is not neccessarily the same Colour object you
are handed back so you can't use "==".

Note also that the selected row draws with the selection colour so you need
to move the selection to see the change.


<user@domain.invalid> wrote in message news:cl33eg$tp8$1@eclipse.org...
> Hi,
>
> After setting the background color of a table cell
> TableItem.getBackground(index) doesnt reflect the changes, it always
> returns the old value.
> I've added an example with a demo of the problem and a workaround
> (commented out). Click on a cell and it should switch between green and
> red for each klick.
>
> Can anyone confirm this problem ?
>
>
> thanks in advance,
> Yves Harms
>
>
> package org.eclipse.swt.snippets;
>
> import org.eclipse.jface.viewers.*;
> import org.eclipse.swt.*;
> import org.eclipse.swt.graphics.*;
> import org.eclipse.swt.layout.*;
> import org.eclipse.swt.widgets.*;
>
> public class Snippet129 { public void createPartControl(Composite
> parent){
> final Color red = parent.getDisplay().getSystemColor(SWT.COLOR_RED);
> final Color green = parent.getDisplay().getSystemColor(SWT.COLOR_GREEN);
> final Table table = new Table(parent, SWT.BORDER);
> //table.setBackground(yellow);
> table.setHeaderVisible(true);
> String[] columnNames = {"a","b","c"}; TableColumn col1 = new
> TableColumn(table, SWT.NONE);
> TableColumn col2 = new TableColumn(table, SWT.NONE); TableColumn col3 =
> new TableColumn(table, SWT.NONE);
> col1.setText(columnNames[0]);
> col2.setText(columnNames[1]);
> col3.setText(columnNames[2]);
> col1.pack();
> col2.pack();
> col3.pack();
> TableItem item = new TableItem(table, SWT.NONE);
> item.setText(new String[] {"white fore/blue back","normal","white
> fore/red back"});
> item.setBackground(0, green);
> item.setBackground(2, red);
> item = new TableItem(table, SWT.NONE);
> item.setText(new String[] {"white fore/blue back","normal","white
> fore/red back"});
> item.setBackground(0, green);
> item.setBackground(2, red);
>
> table.addListener (SWT.MouseDown, new Listener () {
> public void handleEvent (Event event) {
> Rectangle clientArea = table.getClientArea ();
> Point pt = new Point (event.x, event.y);
> int index = table.getTopIndex ();
> while (index < table.getItemCount ()) {
> boolean visible = false;
> TableItem item = table.getItem (index);
> for (int i=0; i < table.getColumnCount(); i++) {
> Rectangle rect = item.getBounds (i);
> if (rect.contains (pt)) { TableItem row = table.getItem(index);
> System.out.println (row.getBackground(i));
>
> if(row.getBackground(i)==red){
> row.setBackground(i, green); }else{ row.setBackground(i, red); }
>
> /*
>
> //WORKAROUND: store colors in data objects
> Object data = row.getData("bgColor");
> Color[] cellColors;
> if(data==null){
> cellColors = new Color[table.getColumnCount()];
> }else{
> System.out.println(data.getClass());
> cellColors = (Color[])data;
>
> }
> //if(cellColors[i]==null||cellColors[i]==red){
> if(row.getBackground(i)==red){
> row.setBackground(i, green);
> cellColors[i]=green;
>
> row.setData("bgColor",cellColors);
> }else{ row.setBackground(i, red);
> cellColors[i]=red;
> row.setData("bgColor",cellColors); }
>
> */
>
> table.redraw();
> }
> if (!visible && rect.intersects (clientArea)) {
> visible = true;
> }
> }
> if (!visible) return;
> index++;
> }
> }
> });
> }
> public static void main(String[] args) {
> Display display = new Display();
> Snippet129 app = new Snippet129();
> Shell shell = new Shell(display);
> shell.setLayout(new FillLayout());
> app.createPartControl(shell); shell.pack();
> shell.open();
>
> while (!shell.isDisposed()) {
> if (!display.readAndDispatch())
> display.sleep();
> }
> display.dispose();
> }
> }
>
Previous Topic:SWT / MVC and menu listeners
Next Topic:Button with image & text - Is it possible??
Goto Forum:
  


Current Time: Sat Oct 31 16:29:06 GMT 2020

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

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

Back to the top