怎样在开发RCP中取得Text中的值?高手快来!我有个程序想取得dialog中的text的值!怎么做最好?会的快来呀!帮帮忙呀!谢了!!!!
[b]问题补充:[/b]
先谢过2位了!我先试试!!
贴一个例子, 这个Dialog是继承自org.eclipse.jface.dialogs.Dialog:
[code="java"]public class TestDiaolog extends Dialog {
private Text nameText;
private String name;
public TestDiaolog(Shell parentShell) {
super(parentShell);
}
public Text getNameText() {
return nameText;
}
public String getName() {
return name;
}
protected Control createDialogArea(Composite parent) {
Composite container = (Composite) super.createDialogArea(parent);
nameText = new Text(container, SWT.BORDER);
nameText.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false));
return container;
}
protected void createButtonsForButtonBar(Composite parent) {
createButton(parent, IDialogConstants.OK_ID, IDialogConstants.OK_LABEL, true);
createButton(parent, IDialogConstants.CANCEL_ID, IDialogConstants.CANCEL_LABEL, false);
}
@Override
protected void okPressed() {
// 如果OK被点击, 给name赋值
name = nameText.getText();
super.okPressed();
}
protected Point getInitialSize() {
return new Point(240, 180);
}
}[/code]下面是测试:
[code="java"] final Display display = Display.getDefault();
final Shell shell = new Shell();
shell.setSize(500, 375);
shell.setText("SWT Application");
//
TestDiaolog dialog = new TestDiaolog(shell);
if (dialog.open() == Dialog.OK) {
MessageDialog.openInformation(shell, "Msg", dialog.getName());
/** 下面这句会抛 org.eclipse.swt.SWTException: Widget is disposed 异常, 因为TestDiaolog此时关闭, Text也随之销毁* */
MessageDialog.openInformation(shell, "Msg", dialog.getNameText().getText());
}
shell.open();
shell.layout();
while (!shell.isDisposed()) {
if (!display.readAndDispatch())
display.sleep();
}
}
[/code]
使用[code="java"]text.getText()[/code],有什么问题吗?
另外,这篇文章讲了"给自定义Dialog加入保留对话框值的功能"
http://www.blogjava.net/dreamstone/archive/2007/08/09/134565.html
不知道对你有帮助没有?
要在你的对话框类中放一个和text对应的实例变量:
[code="java"]private Text nameText;
private String name;
@Override
protected void okPressed() {
name = nameText.getText();
super.okPressed();
}
public String getName() {
return name;
}[/code]
然后在关闭对话框的时候选择给这个实例变量赋值, 上面是当点击OK按钮时给name赋值!这样做是因为dialog.open()方法关闭后, 对话框所持有组件自动销毁, 这时如果直接访问Text组件就会出错!