Community
Participate
Working Groups
From the eclipse.platform.swt newsgroup: I am building a tree table using the new Tree functionality. When setting the background on a TreeItem using SWT 3.1.1, only the item background color in the Tree is set. The rest of the columns are not. In SWT 3.1.0, the entire row background color was set. The reason I need this to work is I have a recursive routine that alternates row color, much like the behaviour of tables in GTK when showGrid is true. I am using Windows, which of course shows a grid instead of alternating colors. I call the routine after the tree is populated and when items are expanded or collapsed. Any answers to this problem are appretiated. If anyone is interested, here is a snippet from my custom viewer: int ri; public void stripeRows(final TreeItem[] items) { ri = 0; Display.getDefault().asyncExec(new Runnable() { public void run() { traverseAndColor(items); } }); } private void traverseAndColor(TreeItem[] items) { for (int i = 0; i < items.length; i++) { TreeItem item = items[i]; if (ri % 2 == 0) { item.setBackground(evenColor); setControlsBackground(item, evenColor); } else { item.setBackground(oddColor); setControlsBackground(item, oddColor); } ri++; if (item.getExpanded()) traverseAndColor(item.getItems()); } } private void setControlsBackground(TreeItem item, Color color) { if (item.getData() != null && item.getData() instanceof Control[]) { Control[] controls = (Control[]) item.getData(); for (Control control : controls) if (control != null) { control.setBackground(color); } } }
Standalone example that reproduces the problem: static Color even, odd; static int pos; public static void traverseAndColor(Display display, TreeItem[] items) { for(int i = 0; i < items.length; i++) { TreeItem item = items[i]; item.setBackground((pos++ & 1) == 0 ? even : odd); if (item.getExpanded()) { traverseAndColor(display, item.getItems()); } } } public static void main(String [] args) { final Display display = new Display(); Shell shell = new Shell(display); shell.setLayout(new FillLayout()); even = display.getSystemColor(SWT.COLOR_GRAY); odd = display.getSystemColor(SWT.COLOR_WHITE); final Tree tree = new Tree(shell, SWT.FULL_SELECTION); TreeColumn c1 = new TreeColumn(tree, SWT.NONE); TreeColumn c2 = new TreeColumn(tree, SWT.NONE); TreeColumn c3 = new TreeColumn(tree, SWT.NONE); for(int i = 0; i < 5; i++) { TreeItem item = new TreeItem(tree, SWT.NONE); item.setText(new String[] { "Hello", "Root", "World" } ); for(int j = 0; j < 5; j++) { TreeItem item2 = new TreeItem(item, SWT.NONE); item2.setText(new String[] { "Hello", "Sub", "World" }); } } c1.pack(); c2.pack(); c3.pack(); final Runnable runnable = new Runnable() { public void run() { pos = 0; traverseAndColor(display, tree.getItems()); }; }; Listener listener = new Listener() { public void handleEvent(Event event) { display.asyncExec(runnable); } }; tree.addListener(SWT.Expand, listener); tree.addListener(SWT.Collapse, listener); runnable.run(); shell.open(); while (!shell.isDisposed()) { if (!display.readAndDispatch()) { display.sleep(); } } display.dispose(); }
Note that to reproduce the problem, you must use SWT.FULL_SELECTION. This was caused by the fix for bug 106289 which has been backed out for 3.1.2. I have verified that with the change backed out, this problem goes away. Marking as FIXED for 3.1.2.