【问题标题】:How to validate a Text with Jface Dialog?如何使用 Jface 对话框验证文本?
【发布时间】:2014-03-06 12:27:52
【问题描述】:

我使用以下代码创建了一个包含两个输入字段的对话框。

 public class CCIDDialog extends TitleAreaDialog {

 private Text ccidText;
 private Text descriptionText;
 private String CCID;
 private String description;


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



public void create() {
    super.create();
    setTitle(_title);
    setMessage("Bitte geben Sie die CCID "+firstchar+"xxxxxxx und eine Beschreibung ein (max. 7-stellig): ", 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);

    createCCID(container);
    createDescription(container);

    return area;
  }

  private void createCCID(Composite container) {
    Label lbtFirstName = new Label(container, SWT.NONE);
    lbtFirstName.setText("CCID (ohne "+firstchar+"): ");

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

    ccidText = new Text(container, SWT.BORDER);

    ccidText.setLayoutData(dataCCID);
  }

  private void createDescription(Composite container) {
    Label lbtLastName = new Label(container, SWT.NONE);
    lbtLastName.setText("Beschreibung: ");

    GridData dataDescription = new GridData();
    dataDescription.grabExcessHorizontalSpace = true;
    dataDescription.horizontalAlignment = GridData.FILL;
    descriptionText = new Text(container, SWT.BORDER);
    descriptionText.setLayoutData(dataDescription);
  }



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

  // save content of the Text fields because they get disposed
  // as soon as the Dialog closes
  private void saveInput() {
      CCID = ccidText.getText();
      description = descriptionText.getText();

  }

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

  public String getCCID() {
    return CCID;
  }

  public String getDescription() {
    return description;
  }
} 

有没有办法验证 ccidtext? 如果用户输入超过 7 个字符,他必须收到通知并且不能继续对话。我在互联网上阅读了很多内容,但找不到解决此问题的方法。

非常感谢您的帮助。

JonasInt

【问题讨论】:

    标签: java validation text dialog jface


    【解决方案1】:

    您可以使用Text.addModifyListener 添加ModifyListener,每次更改文本时都会调用它。您也可以使用Text.addVerifyListener 来添加VerifyListener,这实际上可以防止输入文本。

    对于TitleAreaDialog,您可以调用setMessagesetErrorMessage 在标题区域显示一条消息。

    您可以使用以下方法禁用对话框上的OK 按钮:

    getButton(IDialogConstants.OK_ID).setEnabled(false);
    

    注意:getButton(xxx) 可以返回 null 如果您在对话框构造中过早调用它。在调用createDialogArea 方法之后,在createContents 方法期间创建按钮。

    因此您可以通过覆盖createContents 来访问按钮,如下所示:

    @Override
    protected Control createContents(final Composite parent)
    {
      Control control = super.createContents(parent);
    
      // TODO access buttons here
    
      return control;
    }
    

    【讨论】:

    • 谢谢 :) .setTextLimit(8) 直接用于文本字段 :) 有没有办法设置必须输入最少 1 个字符的字段?
    • 在哪里最好打电话给getButton(IDialogConstants.OK_ID).setEnabled(false);以免回null
    • @kerner1000 通过覆盖 createContents - 查看更新的答案
    • 好的,谢谢,我现在在create 中完成了,也可以。 createDialogArea 没用。现在我看到了,你已经写了,对不起!
    猜你喜欢
    • 2021-10-25
    • 2016-10-25
    • 1970-01-01
    • 2021-04-12
    • 2015-11-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多