在TitleAreaDialog(JFace对话框)中创建ComboBox [英] Create ComboBox within a TitleAreaDialog (JFace dialog)

查看:74
本文介绍了在TitleAreaDialog(JFace对话框)中创建ComboBox的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am trying to create a combo box within a TitleAreaDialog of a JFace Dialog. In the following code am I asking the user for the values for height and width and also the user has to choose the line strength out of the combo box (uneditable). This will be 1, 2, 3 or 4. This is what I have so far:

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()中读取值?

I am now stuck at the method createLineStrength(). How can I input a combobox and then read the value in getLineStrength()?

推荐答案

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" ...

lineHeight.getSelectionIndex() will return the current selection - so 0 for "1", 1 for "2"...

这篇关于在TitleAreaDialog(JFace对话框)中创建ComboBox的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆