eclipse SWT中的org.eclipse.swt.widgets.Text验证装饰器

时间:2013-05-30 07:45:08

标签: java eclipse-plugin swt

为Text / Combo变量启用这样的装饰器的基本方法(没有自定义API)是什么? enter image description here

2 个答案:

答案 0 :(得分:10)

以下是我设法做到的方法:

        //---> input        
        myPackage = new Text(grpConnection, SWT.BORDER);
        myPackage.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));

        //---> input event
        myPackage.addModifyListener(new ModifyListener(){
            // decorator for UI warning
            ControlDecoration decorator;

            /*
             * In this anonymous constructor we will initialize what needs to be initialized only once, namely the decorator.
             */
            {
                decorator = new ControlDecoration(myPackage, SWT.CENTER);
                decorator.setDescriptionText("Not a valid package");
                Image image = FieldDecorationRegistry.getDefault().getFieldDecoration(FieldDecorationRegistry.DEC_ERROR).getImage();
                decorator.setImage(image);
            }

            @Override
            public void modifyText(ModifyEvent e) {
                if (true) { // place your condition here
                    decorator.show();
                }
                else {
                    decorator.hide();
                }
            }
        });

DEC_ERROR FieldDecorationRegistry设置错误图标。

enter image description here

答案 1 :(得分:4)

JFace数据绑定会自动为您设置无效输入:

 ControlDecorationSupport.create(binding, SWT.TOP | SWT.LEFT);

这将使用图标装饰控件,并将工具提示文本设置为验证状态描述。在您的情况下,您不必手动处理所有这些修改侦听器。