带有弹出窗口的Eclipse RCP SWT问题

时间:2012-03-16 04:59:00

标签: java swt windowbuilder

我正在使用WindowBuilder Pro构建一个简单的SWT Eclipse插件。当用户单击工具栏项时,我希望弹出菜单显示我在下面创建的对话框类,以ViewPart为中心。有谁知道如何做到这一点?它在Swing中更加直观......

public class MyApp extends ViewPart {

    public void createPartControl(final Composite arg0) {
        arg0.setLayout(new GridLayout(1, false));

        final ToolBar toolBar = new ToolBar(arg0, SWT.FLAT | SWT.RIGHT);
        toolBar.setLayoutData(new GridData(SWT.LEFT, SWT.CENTER, true, false, 1, 1));

        final ToolItem connectItem = new ToolItem(toolBar, SWT.NONE);
        connectItem.addSelectionListener(new SelectionAdapter() {
            @Override
            public void widgetSelected(final SelectionEvent e) {
                System.out.println("connect button clicked!");
                final Shell newShell = new Shell();
                newShell.setText("Connect to box");
                newShell.setLayout(new GridLayout(2, false));
                newShell.setSize(400, 400);
                newShell.pack();
                newShell.open();
                final ConnectSUVDialog dialog = new ConnectBoxDialog(newShell);
            }
        });
        .
        .
        .
    }
}


public class ConnectSUVDialog
    extends Dialog {
    private Text txtHostName;
    private Text txtUserName;
    private Text txtPassword;

    public ConnectSUVDialog(final Shell parentShell) {
        super(parentShell);
    }

    @Override
    protected Control createDialogArea(final Composite parent) {
        final Composite container = (Composite) super.createDialogArea(parent);
        container.setLayout(new GridLayout(2, false));


        this.txtHostName = new Text(container, SWT.BORDER);
        this.txtHostName.setText("host name");
        this.txtHostName.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, false, false, 1, 1));

        this.txtUserName = new Text(container, SWT.BORDER);
        this.txtUserName.setText("user name");
        this.txtUserName.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, false, false, 1, 1));

        this.txtPassword = new Text(container, SWT.BORDER);
        this.txtPassword.setText("password");
        this.txtPassword.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, false, false, 1, 1));

        return container;
    }

    /**
     * Create contents of the button bar.
     * @param parent
     */
    @Override
    protected void createButtonsForButtonBar(final Composite parent) {
        createButton(parent, IDialogConstants.OK_ID, IDialogConstants.OK_LABEL, true);
        createButton(parent, IDialogConstants.CANCEL_ID, IDialogConstants.CANCEL_LABEL, false);
    }

    /**
     * Return the initial size of the dialog.
     */
    @Override
    protected Point getInitialSize() {
        return new Point(450, 300);
    }

}

1 个答案:

答案 0 :(得分:2)

您无需创建新的shell即可打开对话框。

ConnectSUVDialog dialog = new ConnectSUVDialog(arg0.getShell());
dialog.open();