JFace中Dialog类的使用方法

cyper的例子:
注意dialog.open()返回Dialog.OK而不是SWT.OK

窗体代码:

public class ChooseIntegerDialog extends Dialog {
    private String[] possibleIntegerFields;
    /**
     * checkedIntegerFields is fields separated with comma
     */
    private String checkedIntegerFields;
    private List<Button> buttonList = new ArrayList<Button>();

    public ChooseIntegerDialog(Shell parentShell, String[] possibleIntegerFields) {
        super(parentShell);
        this.possibleIntegerFields = possibleIntegerFields;
    }

    @Override
    protected void configureShell(Shell newShell) {
        super.configureShell(newShell);
        newShell.setSize(300, 200);
        newShell.setLocation(450, 200);
        newShell.setText("Choose Integer Dialog");
    }

    @Override
    protected Control createDialogArea(Composite parent) {
        parent.setLayout(new GridLayout(1, false));
        new Label(parent, SWT.NONE).setText("哪些字段是数值类型(不使用单引号包裹)");

        for (int i = 0; i < possibleIntegerFields.length; i++) {
            Button btn = new Button(parent, SWT.CHECK);
            btn.setText(possibleIntegerFields[i]);
            buttonList.add(btn);
        }

        return super.createDialogArea(parent);
    }

    @Override
    protected void okPressed() {
        StringBuffer sb = new StringBuffer();
        for (Button btn : buttonList) {
            if (btn.getSelection()) {
                sb.append(btn.getText());
                sb.append(",");
            }
        }
        if (sb.length() > 0) {
            sb.setLength(sb.length() - 1);
            checkedIntegerFields = sb.toString();
        }

        super.okPressed();
    }

    public String getCheckedIntegerFields() {
        return checkedIntegerFields;
    }
}
使用代码:
    String[] possibleIntegerFields = 
            REUtil.getREGroupSet(content, "([_a-zA-Z0-9]+)\\s*(?:=|<>|!=)\\s*'[0-9]+'");
    String checkedIntegerFields = null;
    ChooseIntegerDialog integerDialog = new ChooseIntegerDialog(sw.getShell(), possibleIntegerFields);
    if (integerDialog.open() == Dialog.OK) {
        checkedIntegerFields = integerDialog.getCheckedIntegerFields();
    }else{
        return;
    }

以下是转载部分

近期的工作需要一个模态化的对话框,发现JFace中的Dialog正好符合我的要求,并且可以定制的方面也不少,使用起来很方便。下面是一些常用方法,因此在这里记录下来。

 

①设置标题栏名称

protected void configureShell(Shell newShell) {  
    // TODO Auto-generated method stub  
    super.configureShell(newShell);  
    newShell.setText("Hello!");  
}   

 

②设置窗体大小

protected Point getInitialSize() {  
    // TODO Auto-generated method stub  
    return new Point(300,400);  
}   

③取消自带的OK、Cancel按钮

    @Override  
    protected void createButtonsForButtonBar(Composite parent) {  
        // TODO Auto-generated method stub  
    }   

④定义对话框上我们需要的控件

    @Override      
    protected Control createDialogArea(Composite parent) {      
        // TODO Auto-generated method stub      
        Composite container = new Composite(parent, SWT.NONE);      
        container.setBounds(0, 0, 300, 400);      
                  
        Button btn = new Button(container, SWT.NONE);      
        btn.setBounds(10, 10, 55, 20);      
        btn.setText("Click!");      
        // 实现自带“取消”按钮的功能  
        btn.addSelectionListener(new SelectionAdapter(){  
            @Override  
            public void widgetSelected(SelectionEvent e) {  
                // TODO Auto-generated method stub  
                setReturnCode(CANCEL);  
                close();  
            }  
        });  
        return container;      
    }   

⑤居中对齐

      这个最简单了,在创建Dialog的时候指定父窗口shell就可以了!

 

⑥更改Shell样式

    @Override  
    protected void setShellStyle(int newShellStyle) {  
        // TODO Auto-generated method stub  
        // 取消关闭“X”按钮  
        super.setShellStyle(newShellStyle ^ SWT.CLOSE);  
    }   

⑦自定义关闭事件

@Override  
public boolean close() {  
    // TODO Auto-generated method stub  
    super.close();  
    // do something  
    return true;  
}  


你可能感兴趣的:(dialog,jface)