【发布时间】:2018-08-14 04:39:23
【问题描述】:
我正在尝试在 SWT 中模拟 Tab 键按下事件,但我找不到任何方法来执行此操作。
我有一个组合,它包含一个 texfield、一个 ListViewer 和一个按钮。
当我在文本字段中按 Tab 时,我想将焦点设置在按钮上,而不是 ListViewer。
【问题讨论】:
-
您想更改标签顺序。为此使用
setTabList(Control[])
我正在尝试在 SWT 中模拟 Tab 键按下事件,但我找不到任何方法来执行此操作。
我有一个组合,它包含一个 texfield、一个 ListViewer 和一个按钮。
当我在文本字段中按 Tab 时,我想将焦点设置在按钮上,而不是 ListViewer。
【问题讨论】:
setTabList(Control[])
你调查过这个人吗?
我没有尝试过这种事情,但是上面的sn-p改变了tab顺序。
【讨论】:
有两种方法可以解决这个问题:
SWT.Traverse,阻止事件并手动强制焦点在按钮上。以下是两种解决方案的一些代码:
1.
public static void main(String[] args)
{
Display display = new Display();
Shell shell = new Shell(display);
shell.setText("StackOverflow");
shell.setLayout(new FillLayout());
Button button = new Button(shell, SWT.PUSH);
button.setText("Button");
Text text = new Text(shell, SWT.BORDER);
new Text(shell, SWT.BORDER).setText("This won't get focused.");
shell.setTabList(new Control[] {text, button});
shell.pack();
shell.open();
while (!shell.isDisposed())
{
if (!display.readAndDispatch())
{
display.sleep();
}
}
display.dispose();
}
2.
public static void main(String[] args)
{
Display display = new Display();
Shell shell = new Shell(display);
shell.setText("StackOverflow");
shell.setLayout(new FillLayout());
Button button = new Button(shell, SWT.PUSH);
button.setText("Button");
Text text = new Text(shell, SWT.BORDER);
new Text(shell, SWT.BORDER).setText("This won't get focused.");
text.addListener(SWT.Traverse, e -> {
if(e.detail == SWT.TRAVERSE_TAB_NEXT)
{
e.doit = false;
button.forceFocus();
}
});
shell.pack();
shell.open();
while (!shell.isDisposed())
{
if (!display.readAndDispatch())
{
display.sleep();
}
}
display.dispose();
}
【讨论】: