【发布时间】:2014-06-06 06:59:28
【问题描述】:
我要做的是:当我更改Group小部件的标题时(通过调用setText()方法),我需要检查标题,如果包含某些字符,我会做一些事情。
现在的问题是,如何监听 Group widget 的 text modify 事件?
我尝试了addListener(SWT.Modify, listener); 方法,(参见下面的示例),它不起作用。
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Group;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.Shell;
public class Test {
public static void main(String[] args)
{
final Display display = new Display();
Shell shell = new Shell(display);
shell.setLayout(new GridLayout(1, false));
shell.setText("Stackoverflow");
final Group group = new Group(shell, SWT.NONE);
group.setText("title0");
group.addListener(SWT.Modify, new Listener() {
@Override
public void handleEvent(Event event) {
// TODO Auto-generated method stub
System.out.println("Group title changed");
}
});
Button btn = new Button(shell, SWT.NONE);
btn.setText("Press to set title of Group.");
btn.addSelectionListener(new SelectionAdapter() {
@Override
public void widgetSelected(SelectionEvent e) {
// TODO Auto-generated method stub
group.setText("title1");
}
});
shell.pack();
shell.open();
while (!shell.isDisposed())
{
if (!display.readAndDispatch())
{
display.sleep();
}
}
display.dispose();
}
}
【问题讨论】:
-
Group在调用setText时不会生成任何事件。 -
您应该在致电
setText(String)之前进行检查。您甚至可以创建一个辅助函数来执行此操作。