|
|
Re: Table: set "focused" row [message #730972 is a reply to message #730822] |
Thu, 29 September 2011 16:01 |
Grant Gayed 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 |
Henno Vermeulen 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
|
|
|
Powered by
FUDForum. Page generated in 0.03422 seconds