【问题标题】:Create ComboBox within a TitleAreaDialog (JFace dialog)在 TitleAreaDialog(JFace 对话框)中创建 ComboBox
【发布时间】:2014-09-09 16:04:33
【问题描述】:

我正在尝试在 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() 中的值?

【问题讨论】:

    标签: java combobox swt jface


    【解决方案1】:
    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”...

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-02-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多