在TitleAreaDialog(JFace对话框)中创建ComboBox

时间:2014-09-08 06:35:29

标签: java combobox swt jface

我正在尝试在JFace对话框的TitleAreaDialog中创建一个组合框。 在下面的代码中,我向用户询问高度和宽度的值,并且用户还必须从组合框中选择线强度(不可编辑)。这将是1,2,3或4.这是我到目前为止:

import org.eclipse.jface.dialogs.TitleAreaDialog;
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;

public class RectDialog extends TitleAreaDialog {

    private Text txtWidth;
    private Text txtHeight;
    private Text txtLineStrength;

    private String width;
    private String height;
    private String lineStrength;

    public RectDialog(Shell parentShell) {
        super(parentShell);
    }

    @Override
    public void create() {
        super.create();
        setTitle("New Rectangle");
        // setMessage("", IMessageProvider.INFORMATION);
    }

    @Override
    protected Control createDialogArea(Composite parent) {
        Composite area = (Composite) super.createDialogArea(parent);
        Composite container = new Composite(area, SWT.NONE);
        container.setLayoutData(new GridData(GridData.FILL_BOTH));
        GridLayout layout = new GridLayout(2, false);
        container.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
        container.setLayout(layout);

        createWidth(container);
        createHeight(container);
        createLineStrength(container);

        return area;
    }

    private void createLineStrength(Composite container) {
        // TODO: Create line strength
    }

    private void createHeight(Composite container) {
        Label lbtHeight = new Label(container, SWT.NONE);
        lbtHeight.setText("Height");

        GridData dataHeight = new GridData();
        dataHeight.grabExcessHorizontalSpace = true;
        dataHeight.horizontalAlignment = GridData.FILL;

        txtHeight = new Text(container, SWT.BORDER);
        txtHeight.setLayoutData(dataHeight);
    }

    private void createWidth(Composite container) {
        Label lbtWidth = new Label(container, SWT.NONE);
        lbtWidth.setText("Height");

        GridData dataWidth = new GridData();
        dataWidth.grabExcessHorizontalSpace = true;
        dataWidth.horizontalAlignment = GridData.FILL;

        txtWidth = new Text(container, SWT.BORDER);
        txtWidth.setLayoutData(dataWidth);
    }

    @Override
    protected boolean isResizable() {
        return true;
    }

    private void saveInput() {
        width = txtWidth.getText();
        height = txtHeight.getText();
//      lineStrength = txtLineStrength.getText();
    }

    @Override
    protected void okPressed() {
        saveInput();
        super.okPressed();
    }

    public String getWidth() {
        return width;
    }

    public String getHeight() {
        return height;
    }

    public String getLineStrength() {
        return lineStrength;
    }
}

我现在停留在createLineStrength()方法中。如何输入组合框然后读取getLineStrength()中的值?

1 个答案:

答案 0 :(得分:0)

Combo lineHeight;

lineHeight = new Combo(parent, SWT.READ_ONLY | SWT.BORDER);

lineHeight.setLayoutData(new GridData(SWT.LEAD, SWT.CENTER, false, false));

String [] choices = {"1", "2", "3", "4"};

lineHeight.setItems(choices);

lineHeight.select(0);  // Select "1" by default

lineHeight.getSelectionIndex()将返回当前选择 - 因此0为" 1",1为" 2" ...