Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » Standard Widget Toolkit (SWT) » Table.setSortColumn sets column background to grey
Table.setSortColumn sets column background to grey [message #753889] Sun, 30 October 2011 07:11 Go to next message
nimonic   is currently offline nimonic Friend
Messages: 6
Registered: October 2011
Junior Member
When running Table.setSortColumn the background of the selected column is set to grey. I want the background to stay white similar to the behaviour in Windows Explorer, Outlook etc. Is this possible?

I have tried Table.setBackground but it didn't work.

I am running SWT 3.7.1 on Windows 7.
Re: Table.setSortColumn sets column background to grey [message #753931 is a reply to message #753889] Sun, 30 October 2011 17:55 Go to previous messageGo to next message
Thomas Singer is currently offline Thomas SingerFriend
Messages: 75
Registered: July 2009
Member
+1, especially because our tables are sorted by multiple columns.
Re: Table.setSortColumn sets column background to grey [message #753951 is a reply to message #753931] Mon, 31 October 2011 07:02 Go to previous messageGo to next message
Thomas Schindl is currently offline Thomas SchindlFriend
Messages: 6651
Registered: July 2009
Senior Member
did you try to set the item.setBackground(int,Color) which will overrule
the native coloring?

Tom

Am 30.10.11 18:55, schrieb Thomas Singer:
> +1, especially because our tables are sorted by multiple columns.
Re: Table.setSortColumn sets column background to grey [message #753990 is a reply to message #753951] Mon, 31 October 2011 11:20 Go to previous messageGo to next message
nimonic   is currently offline nimonic Friend
Messages: 6
Registered: October 2011
Junior Member
I've tried the following:

If I set the table background to white there is no change.
If I set the table background to red the background changes to red.
If I set the table background to 'widgetBackground' the selected column changes to white and all other columns change to grey!

Could this be a bug?

My test code below:

import org.eclipse.swt.SWT;


public class SetSortColumnTest extends Shell {
	private static Display display;
	private Table table;
	private Color white;
	private Color red;
	private Color widgetBackground;

	/**
	 * Launch the application.
	 * @param args
	 */
	public static void main(String args[]) {
		try {
			display = Display.getDefault();
			SetSortColumnTest shell = new SetSortColumnTest(display);
			shell.open();
			shell.layout();
			while (!shell.isDisposed()) {
				if (!display.readAndDispatch()) {
					display.sleep();
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * Create the shell.
	 * @param display
	 */
	public SetSortColumnTest(Display display) {
		super(display, SWT.SHELL_TRIM);
				
		createContents();
	}

	/**
	 * Create contents of the shell.
	 */
	protected void createContents() {
		setText("SWT Application");
		setSize(327, 308);
		setLayout(new GridLayout(1, false));
		
		ToolBar toolBar = new ToolBar(this, SWT.FLAT | SWT.RIGHT);
		toolBar.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
		
		ToolItem tltmNormal = new ToolItem(toolBar, SWT.NONE);
		tltmNormal.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				table.setBackground(null);
			}
		});
		tltmNormal.setText("Normal");
		
		ToolItem tltmWhite = new ToolItem(toolBar, SWT.NONE);
		tltmWhite.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				table.setBackground(white);
			}
		});
		tltmWhite.setText("White");
		
		ToolItem tltmRed = new ToolItem(toolBar, SWT.NONE);
		tltmRed.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				table.setBackground(red);
			}
		});
		tltmRed.setText("Red");
		
		ToolItem tltmWidgetBackground = new ToolItem(toolBar, SWT.NONE);
		tltmWidgetBackground.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				table.setBackground(widgetBackground);
			}
		});
		tltmWidgetBackground.setText("Widget Background");
		table = new Table(this, SWT.BORDER | SWT.FULL_SELECTION);
		table.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1));
		table.setHeaderVisible(true);
		table.setLinesVisible(true);
		
		TableColumn tblclmnColumn1 = new TableColumn(table, SWT.NONE);
		tblclmnColumn1.setWidth(100);
		tblclmnColumn1.setText("Column 1");
		
		TableColumn tblclmnColumn2 = new TableColumn(table, SWT.NONE);
		tblclmnColumn2.setWidth(100);
		tblclmnColumn2.setText("Column 2");
		
		TableColumn tblclmnColumn3 = new TableColumn(table, SWT.NONE);
		tblclmnColumn3.setWidth(100);
		tblclmnColumn3.setText("Column3");			
		
		table.setSortColumn(tblclmnColumn2);
		table.setSortDirection(SWT.UP);
		
		white = display.getSystemColor(SWT.COLOR_WHITE);
		red = display.getSystemColor(SWT.COLOR_RED);		
		widgetBackground = display.getSystemColor(SWT.COLOR_WIDGET_BACKGROUND);	
	}

	@Override
	protected void checkSubclass() {
		// Disable the check that prevents subclassing of SWT components
	}

}


Re: Table.setSortColumn sets column background to grey [message #754001 is a reply to message #753951] Mon, 31 October 2011 11:38 Go to previous messageGo to next message
Thomas Singer is currently offline Thomas SingerFriend
Messages: 75
Registered: July 2009
Member
Tom Schindl wrote on Mon, 31 October 2011 03:02
did you try to set the item.setBackground(int,Color) which will overrule
the native coloring?

No, I've did not tried, because I don't want to override the normal native coloring. I just want the sorting column background to be the same platform specific coloring like the non-sorting columns.
Re: Table.setSortColumn sets column background to grey [message #754047 is a reply to message #754001] Mon, 31 October 2011 15:15 Go to previous messageGo to next message
Marco Maccaferri is currently offline Marco MaccaferriFriend
Messages: 147
Registered: July 2009
Senior Member
On 31/10/2011 12:38 Thomas Singer ha scritto:

> Tom Schindl wrote on Mon, 31 October 2011 03:02
>> did you try to set the item.setBackground(int,Color) which will overrule
>> the native coloring?
>
> No, I've did not tried, because I don't want to override the normal native
> coloring. I just want the sorting column background to be the same
platform
> specific coloring like the non-sorting columns.

A couple of tricks:

1. Add an SWT.EraseItem event listener to the Table and just fill with
the background color:

table.addListener(SWT.EraseItem, new Listener() {
@Override
public void handleEvent(Event event) {
event.gc.setBackground(((TableItem) event.item).getBackground());
event.gc.fillRectangle(event.getBounds());
}
});

2. Set the item background to a slightly different color than the
platform default, like if platform default is white (255,255,255) set it
to 254,255,255 or 254,254,254:

RGB rgb =
Display.getDefault().getSystemColor(SWT.COLOR_LIST_BACKGROUND).getRGB();
if (rgb.red > 0) {
rgb.red--;
}
if (rgb.green > 0) {
rgb.green--;
}
if (rgb.blue > 0) {
rgb.blue--;
}
Color rowColor = new Color(Display.getDefault(), rgb);

Both of these disable the sort background, option 1 enables the owner
draw events so it may have some performance hits.

Hope this helps.

Regards,
Marco.
Re: Table.setSortColumn sets column background to grey [message #754098 is a reply to message #754047] Mon, 31 October 2011 18:35 Go to previous messageGo to next message
nimonic   is currently offline nimonic Friend
Messages: 6
Registered: October 2011
Junior Member
Thanks for your reply.

I'm not to keen on the first method if it has performance problems.

I've tried the second method but it didn't work. The selected column remained grey.

Test code below:

import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.graphics.RGB;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableColumn;
import org.eclipse.swt.widgets.ToolBar;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.widgets.ToolItem;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;


public class SetSortColumnTest extends Shell {
	private static Display display;
	private Table table;
	private Color white;
	private Color red;
	private Color widgetBackground;
	private Color rowColor;
	private RGB rgb;

	/**
	 * Launch the application.
	 * @param args
	 */
	public static void main(String args[]) {
		try {
			display = Display.getDefault();
			SetSortColumnTest shell = new SetSortColumnTest(display);
			shell.open();
			shell.layout();
			while (!shell.isDisposed()) {
				if (!display.readAndDispatch()) {
					display.sleep();
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * Create the shell.
	 * @param display
	 */
	public SetSortColumnTest(Display display) {
		super(display, SWT.SHELL_TRIM);
				
		createContents();
	}

	/**
	 * Create contents of the shell.
	 */
	protected void createContents() {
		setText("SWT Application");
		setSize(327, 308);
		setLayout(new GridLayout(1, false));
		
		ToolBar toolBar = new ToolBar(this, SWT.FLAT | SWT.RIGHT);
		toolBar.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
		
		ToolItem tltmNormal = new ToolItem(toolBar, SWT.NONE);
		tltmNormal.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				table.setBackground(null);
			}
		});
		tltmNormal.setText("Normal");
		
		ToolItem tltmWhite = new ToolItem(toolBar, SWT.NONE);
		tltmWhite.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				table.setBackground(white);
			}
		});
		tltmWhite.setText("White");
		
		ToolItem tltmRed = new ToolItem(toolBar, SWT.NONE);
		tltmRed.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				table.setBackground(red);
			}
		});
		tltmRed.setText("Red");
		
		ToolItem tltmWidgetBackground = new ToolItem(toolBar, SWT.NONE);
		tltmWidgetBackground.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				table.setBackground(widgetBackground);
			}
		});
		tltmWidgetBackground.setText("Widget Background");
		
		ToolItem tltmNewRgb = new ToolItem(toolBar, SWT.NONE);
		tltmNewRgb.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				table.setBackground(rowColor);
			}
		});
		tltmNewRgb.setText("New RGB");
		table = new Table(this, SWT.BORDER | SWT.FULL_SELECTION);
		table.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1));
		table.setHeaderVisible(true);
		table.setLinesVisible(true);
		
		TableColumn tblclmnColumn1 = new TableColumn(table, SWT.NONE);
		tblclmnColumn1.setWidth(100);
		tblclmnColumn1.setText("Column 1");
		
		TableColumn tblclmnColumn2 = new TableColumn(table, SWT.NONE);
		tblclmnColumn2.setWidth(100);
		tblclmnColumn2.setText("Column 2");
		
		TableColumn tblclmnColumn3 = new TableColumn(table, SWT.NONE);
		tblclmnColumn3.setWidth(100);
		tblclmnColumn3.setText("Column3");			
		
		table.setSortColumn(tblclmnColumn2);
		table.setSortDirection(SWT.UP);
		
		white = display.getSystemColor(SWT.COLOR_WHITE);
		red = display.getSystemColor(SWT.COLOR_RED);		
		widgetBackground = display.getSystemColor(SWT.COLOR_WIDGET_BACKGROUND);	
		
		rgb = Display.getDefault().getSystemColor(SWT.COLOR_LIST_BACKGROUND).getRGB();
		
		System.out.println("red: " + rgb.red);
		System.out.println("green: " + rgb.green);
		System.out.println("blue: " + rgb.blue);
		
		rowColor = new Color(Display.getDefault(), 254, 254, 254);
	}

	@Override
	protected void checkSubclass() {
		// Disable the check that prevents subclassing of SWT components
	}

}

Re: Table.setSortColumn sets column background to grey [message #754179 is a reply to message #754098] Tue, 01 November 2011 08:26 Go to previous messageGo to next message
Marco Maccaferri is currently offline Marco MaccaferriFriend
Messages: 147
Registered: July 2009
Senior Member
On 31/10/2011 19:35 nimonic ha scritto:

> I'm not to keen on the first method if it has performance problems.

It depends on a number of factors, but in my experience it won't be
noticed anyway.

> I've tried the second method but it didn't work. The selected column remained grey.
>
> Test code below:

No, I see you set the table background, sorry I wasn't clear, you need
to set the TableItem background. Whenever you create a new TableItem
call TableItem.setBackground(rowBackground).


Regards,
Marco.
Re: Table.setSortColumn sets column background to grey [message #754866 is a reply to message #754179] Fri, 04 November 2011 16:56 Go to previous message
nimonic   is currently offline nimonic Friend
Messages: 6
Registered: October 2011
Junior Member
Thanks again for your help but it's still not meeting my requirement.

Method one doesn't seem to work at all for me.

Method two only changes the background colour of table items so the rest of the selected column is still displayed in grey.

My code is below.

BTW does anyone know why the SWT table behaves like this? I can't recall ever seeing this behaviour in a windows application so I don't think it is native behaviour of a table widget.

import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.graphics.RGB;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableColumn;
import org.eclipse.swt.widgets.TableItem;
import org.eclipse.swt.widgets.ToolBar;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.widgets.ToolItem;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;

public class SetSortColumnTest extends Shell {
	private static Display display;
	private Table table;
	private Color white;
	private Color red;
	private Color widgetBackground;
	private Color rowColor;
	private RGB rgb;

	/**
	 * Launch the application.
	 * 
	 * @param args
	 */
	public static void main(String args[]) {
		try {
			display = Display.getDefault();
			SetSortColumnTest shell = new SetSortColumnTest(display);
			shell.open();
			shell.layout();
			while (!shell.isDisposed()) {
				if (!display.readAndDispatch()) {
					display.sleep();
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * Create the shell.
	 * 
	 * @param display
	 */
	public SetSortColumnTest(Display display) {
		super(display, SWT.SHELL_TRIM);

		createContents();
	}

	/**
	 * Create contents of the shell.
	 */
	protected void createContents() {
		setText("SWT Application");
		setSize(327, 308);
		setLayout(new GridLayout(1, false));

		ToolBar toolBar = new ToolBar(this, SWT.FLAT | SWT.RIGHT);
		toolBar.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false,
				1, 1));

		ToolItem tltmNormal = new ToolItem(toolBar, SWT.NONE);
		tltmNormal.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				table.setBackground(null);
			}
		});
		tltmNormal.setText("Normal");

		ToolItem tltmWhite = new ToolItem(toolBar, SWT.NONE);
		tltmWhite.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				table.setBackground(white);
			}
		});
		tltmWhite.setText("White");

		ToolItem tltmRed = new ToolItem(toolBar, SWT.NONE);
		tltmRed.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				table.setBackground(red);
			}
		});
		tltmRed.setText("Red");

		ToolItem tltmWidgetBackground = new ToolItem(toolBar, SWT.NONE);
		tltmWidgetBackground.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				table.setBackground(widgetBackground);
			}
		});
		tltmWidgetBackground.setText("Widget Background");

		ToolItem tltmNewRgb = new ToolItem(toolBar, SWT.NONE);
		tltmNewRgb.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				table.setBackground(rowColor);
			}
		});
		tltmNewRgb.setText("New RGB");
		table = new Table(this, SWT.BORDER | SWT.FULL_SELECTION);
		table.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1));
		table.setHeaderVisible(true);
		table.setLinesVisible(true);

		table.addListener(SWT.EraseItem, new Listener() {
			@Override
			public void handleEvent(Event event) {
				System.out.println("Erase Item event has fired.");
				event.gc.setBackground(((TableItem) event.item).getBackground());
				event.gc.fillRectangle(event.getBounds());
			}
		});

		TableColumn tblclmnColumn1 = new TableColumn(table, SWT.NONE);
		tblclmnColumn1.setWidth(100);
		tblclmnColumn1.setText("Column 1");

		TableColumn tblclmnColumn2 = new TableColumn(table, SWT.NONE);
		tblclmnColumn2.setWidth(100);
		tblclmnColumn2.setText("Column 2");

		TableColumn tblclmnColumn3 = new TableColumn(table, SWT.NONE);
		tblclmnColumn3.setWidth(100);
		tblclmnColumn3.setText("Column3");

		table.setSortColumn(tblclmnColumn2);
		table.setSortDirection(SWT.UP);

		TableItem tableItem = new TableItem(table, SWT.NONE);
		tableItem.setText("New TableItem");

		TableItem tableItem_1 = new TableItem(table, SWT.NONE);
		tableItem_1.setText("New TableItem");

		TableItem tableItem_2 = new TableItem(table, SWT.NONE);
		tableItem_2.setText("New TableItem");

		white = display.getSystemColor(SWT.COLOR_WHITE);
		red = display.getSystemColor(SWT.COLOR_RED);
		widgetBackground = display.getSystemColor(SWT.COLOR_WIDGET_BACKGROUND);

		rgb = Display.getDefault().getSystemColor(SWT.COLOR_LIST_BACKGROUND)
				.getRGB();

		System.out.println("red: " + rgb.red);
		System.out.println("green: " + rgb.green);
		System.out.println("blue: " + rgb.blue);

		rowColor = new Color(Display.getDefault(), 254, 254, 254);

		tableItem.setBackground(rowColor);
		tableItem_1.setBackground(rowColor);
		tableItem_2.setBackground(rowColor);

	}

	@Override
	protected void checkSubclass() {
		// Disable the check that prevents subclassing of SWT components
	}

}



Previous Topic:Cairo on HPUX. "Unable to load graphics library" Exception when running SWT application on
Next Topic:Widget transparency problems. Been stuck for weeks.
Goto Forum:
  


Current Time: Thu Apr 25 07:30:15 GMT 2024

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

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

Back to the top