Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » Standard Widget Toolkit (SWT) » Table: set "focused" row
Table: set "focused" row [message #728708] Fri, 23 September 2011 17:50 Go to next message
Thomas Singer is currently offline Thomas SingerFriend
Messages: 75
Registered: July 2009
Member
On Windows, my SWT tables show a dotted border around the "focused" row. This seems to always be the first row initially, not matter what entry I select initially. How can I set the "focused" row to match the selected row?
Re: Table: set "focused" row [message #730822 is a reply to message #728708] Thu, 29 September 2011 09:22 Go to previous messageGo to next message
Thomas Singer is currently offline Thomas SingerFriend
Messages: 75
Registered: July 2009
Member
Does no one have an idea?
Re: Table: set "focused" row [message #730972 is a reply to message #730822] Thu, 29 September 2011 16:01 Go to previous messageGo to next message
Grant Gayed is currently offline Grant GayedFriend
Messages: 2150
Registered: July 2009
Senior Member
Hi,

I see what you're describing. I don't think this is a bug, as there
isn't necessarily a link between the selected item(s) and the focus
item. The focus item is just the place from which keyboard navigations
are resolved.

There isn't a way to set the focus item. A snippet like the one below
can be used to draw focus on an item, but windows still considers the
top item to have the logical focus. I couldn't find an existing feature
request to enable this, so if you want you can log one at
https://bugs.eclipse.org/bugs/enter_bug.cgi?product=Platform&component=SWT&bug_severity=enhancement
(I don't know if this is natively supported on all platforms).

public static void main(String[] args) {
final Display display = new Display();
Shell shell = new Shell(display);
shell.setLayout(new GridLayout());
new Text(shell, SWT.SINGLE).setLayoutData(new GridData(100,30));
final Table table = new Table(shell, SWT.MULTI);
table.setLayoutData(new GridData(200,200));
new TableItem(table, SWT.NONE).setText("One");
new TableItem(table, SWT.NONE).setText("Two");
new TableItem(table, SWT.NONE).setText("Three");
table.select(1);
table.addListener(SWT.EraseItem, new Listener() {
public void handleEvent(Event event) {
int index = table.indexOf((TableItem)event.item);
if (index == 1 && table.isFocusControl()) {
event.detail |= SWT.FOCUSED;
} else {
event.detail &= ~SWT.FOCUSED;
}
}
});
shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch()) display.sleep();
}
display.dispose();
}

Grant


On 9/29/2011 5:22 AM, Thomas Singer wrote:
> Does no one have an idea?
Re: Table: set "focused" row [message #1710559 is a reply to message #730972] Wed, 07 October 2015 14:05 Go to previous message
Henno Vermeulen is currently offline Henno VermeulenFriend
Messages: 126
Registered: July 2009
Senior Member
Sorry for bringing up an old post, but I encountered the same issue and found a solution. I'm putting my solution here just in case someone else finds this post.

My solution involves using an ugly work around: post mouse events that simulate a click on the item:

import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableItem;
import org.eclipse.swt.widgets.Widget;

/**
 * Utility class for tables.
 * 
 * @author Henno Vermeulen
 */
public final class Tables {

	private Tables() {
	}

	/**
	 * Shows and selects the table <code>item</code> and lets it get the
	 * keyboard focus.
	 * 
	 * <p>
	 * Windows has a "focused" row (drawn with a dotted line) that can be moved
	 * independently of the selected row (when pressing ctrl + up/down). This
	 * method ensures that this focused row is made equal to the selected row so
	 * that the selection does not jump to an unwanted location when the user
	 * uses the up or down arrow on the keyboard.
	 * 
	 * <p>
	 * Implementation detail: there is no API to get/set the focused row (see
	 * also <a href="https://www.eclipse.org/forums/index.php/t/241852/">this
	 * post</a> and <a href=
	 * "http://stackoverflow.com/questions/8852574/swt-table-how-to-set-get-focused-row">
	 * this one</a>), so we use a filthy hack: faking a mouse click in the
	 * table.
	 */
	public static void selectAndFocus(TableItem item) {
		fakeMouseClickTableItem(item);
	}

	private static void fakeMouseClickTableItem(TableItem item) {
		Table table = item.getParent();
		table.showItem(item);

		Point cursorLocation = item.getDisplay().getCursorLocation();
		fakeMouseClick(table, getCenter(item.getBounds(0)));
		item.getDisplay().setCursorLocation(cursorLocation);
	}

	private static Point getCenter(Rectangle bounds) {
		return new Point(bounds.x + bounds.width / 2,
				bounds.y + bounds.height / 2);
	}

	/**
	 * Actually moves the mouse cursor to the given <code>location</code> so
	 * that the OS gives the correct click behavior.
	 */
	private static void fakeMouseClick(Control control, Point location) {
		control.getDisplay().setCursorLocation(control.toDisplay(location));
		control.getDisplay()
				.post(createMouseEvent(SWT.MouseDown, control, location));
		control.getDisplay()
				.post(createMouseEvent(SWT.MouseUp, control, location));
	}

	public static Event createMouseEvent(int type, Widget widget,
			Point position) {
		Event e = new Event();
		e.display = widget.getDisplay();
		e.widget = widget;
		e.type = type;
		e.x = position.x;
		e.y = position.y;
		e.count = 1;
		e.button = 1;
		e.doit = true;
		return e;
	}
}

[Updated on: Wed, 07 October 2015 14:10]

Report message to a moderator

Previous Topic:FocusLost event raised when entering a Text
Next Topic:font display issues with mars/centos6
Goto Forum:
  


Current Time: Tue Mar 19 06:47:40 GMT 2024

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

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

Back to the top