如何选择Java SWT Composite? [英] How do I make a Java SWT Composite selectable?
问题描述
我正在使用SWT库在eclipse中创建一个向导页面。到目前为止,我已经成功创建了页面但只有一个问题: - SWT复合没有被选中。我的向导页面包含一个scrollledComposite,其中包含多个网格方式的复合体。每个复合包含一个浏览器,就像一个示例模板。我想要的是当我选择一个包含浏览器的复合材料时,它应该被选中并且激活完成按钮。
现在,我怀疑的是,我如何制作包含它的复合材料可选择为复合的浏览器不包含selectionListener。或者更好的是,有没有办法让浏览器可选?
示例代码将不胜感激。
我是菜鸟,这是我的第一个问题所以请原谅我有任何错误。
I'm creating a wizard page in eclipse using SWT libraries. So far I have succeeded in creating the page but with only one issue:- SWT composite doesn't get selected. My wizard page contains a scrolledComposite containing multiple composites in a grid fashion. Each composite contains a browser just like a sample template. What i want is when i select a composite containing browser it should get selected and the finish button activated.
Now, my doubt is, how do i make the composite containing the browser selectable as a composite doesn't contain selectionListener. Or better still, is there a way to make a browser selectable?
Sample code would be appreciated.
I'm a noob and this is my first question so please forgive me for any mistakes in question.
推荐答案
经过一些研究,我发现有一个系统特定的错误,因为它不会到达FocusListener代码。请参阅此处的错误 https://bugs.eclipse.org/bugs/show_bug.cgi?id=84532
以下代码包含SWT浏览器可以获得焦点的解决方法。
After doing some research, I found there was a system specific bug due to which it wouldn't reach the FocusListener code. See bug here https://bugs.eclipse.org/bugs/show_bug.cgi?id=84532
The following code contains workaround through which a SWT Browser can get focus.
public static void main (String [] args) {
Display.setAppName("Stackoverflow");
final Display display = new Display();
final Shell shell = new Shell(display);
shell.setSize(500, 400);
shell.setLayout(new FillLayout());
final Composite composite = new Composite(shell, SWT.BORDER);
composite.setLayout(new GridLayout());
final Browser browser = new Browser(composite, SWT.NONE);
browser.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
final Button finish = new Button(shell, SWT.PUSH);
finish.setText("Finish");
finish.setEnabled(false);
finish.setLayoutData(new GridData(SWT.FILL, SWT.FILL, false, false));
browser.setUrl("google.com");
final FocusListener listener = new FocusListener() {
@Override
public void focusLost(FocusEvent e) {
System.out.println("Focus lost");
}
@Override
public void focusGained(FocusEvent e) {
System.out.println("Focus gained");
finish.setEnabled(true);
}
};
/*
* workaround for bug https://bugs.eclipse.org/bugs/show_bug.cgi?id=84532
*/
Listener deactivateListener = new Listener() {
@Override
public void handleEvent(Event event) {
listener.focusLost(new FocusEvent(event));
}
};
Listener activateListener = new Listener() {
@Override
public void handleEvent(Event event) {
listener.focusGained(new FocusEvent(event));
}
};
browser.addListener(SWT.Deactivate, deactivateListener);
browser.addListener(SWT.Activate, activateListener);
finish.addSelectionListener(new SelectionAdapter() {
public void widgetSelected(SelectionEvent e){
System.out.println("OK");
}
});
shell.open();
while (!shell.isDisposed())
{
if (!display.readAndDispatch())
display.sleep();
}
display.dispose();
}
现在浏览器确实获得了我的按钮被激活的焦点,但有没有办法将其显示为已选中?
Now the browser does get focus through which my button gets activated but is there any way to show it as selected?
这篇关于如何选择Java SWT Composite?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!