iteye_18435 2008-08-07 16:23
浏览 157
已采纳

Eclipse RCP

怎样在开发RCP中取得Text中的值?高手快来!我有个程序想取得dialog中的text的值!怎么做最好?会的快来呀!帮帮忙呀!谢了!!!!
[b]问题补充:[/b]
先谢过2位了!我先试试!!

  • 写回答

4条回答 默认 最新

  • iteye_14762 2008-08-10 10:59
    关注

    贴一个例子, 这个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]

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥15 smptlib使用465端口发送邮件失败
  • ¥200 总是报错,能帮助用python实现程序实现高斯正反算吗?有偿
  • ¥15 对于squad数据集的基于bert模型的微调
  • ¥15 为什么我运行这个网络会出现以下报错?CRNN神经网络
  • ¥20 steam下载游戏占用内存
  • ¥15 CST保存项目时失败
  • ¥15 树莓派5怎么用camera module 3啊
  • ¥20 java在应用程序里获取不到扬声器设备
  • ¥15 echarts动画效果的问题,请帮我添加一个动画。不要机器人回答。
  • ¥15 Attention is all you need 的代码运行