How to combine and validate two text fields for a swt dialog?

允我心安 提交于 2019-12-07 07:44:34

问题


i have another question. I use a ModifyListener for one textfield to activate and deactivate the OK-Button in a swt dialog. It works great.

Now I want to add a ModifyListener for another textfield. I want that the OK-Button only is activated if in both text fields is min one char.

This is the code of the two fields:

descriptionText.addModifyListener(new ModifyListener(){

    public void modifyText(ModifyEvent e) {
        Text text = (Text) e.widget;

        if (text.getText().length() == 0) {

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

        if (text.getText().length() >= 1) {

            getButton(IDialogConstants.OK_ID).setEnabled(true);
        }
    }
});

}

the second field:

ccidText.addModifyListener(new ModifyListener(){

        public void modifyText(ModifyEvent e) {
            Text text = (Text) e.widget;

            if (text.getText().length() == 0) {
        getButton(IDialogConstants.OK_ID).setEnabled(false);

            }
            if (text.getText().length() >= 1){              
                    getButton(IDialogConstants.OK_ID).setEnabled(true);
            }
        }
    });
}

I know that it doesn´t work because there are no dependencies between the two buttons. How can i combine it?

I want to set the ok-button false while both modifylistener detect a char. If i delete all chars in one testfield the button must be deactivated again.

Thank u.


回答1:


You can use the same Listener for both Text fields and add it for SWT.KeyUp:

public static void main(String[] args)
{
    final Display display = new Display();
    Shell shell = new Shell(display);
    shell.setText("StackOverflow");
    shell.setLayout(new FillLayout(SWT.VERTICAL));

    final Text first = new Text(shell, SWT.BORDER);
    final Text second = new Text(shell, SWT.BORDER);
    final Button button = new Button(shell, SWT.PUSH);
    button.setText("disabled");
    button.setEnabled(false);

    Listener listener = new Listener()
    {
        @Override
        public void handleEvent(Event e)
        {
            String firstString = first.getText();
            String secondString = second.getText();

            button.setEnabled(!isEmpty(firstString) && !isEmpty(secondString));
            button.setText(button.isEnabled() ? "enabled" : "disabled");
        }
    };

    first.addListener(SWT.KeyUp, listener);
    second.addListener(SWT.KeyUp, listener);

    shell.pack();
    shell.setSize(300, shell.getSize().y);
    shell.open();

    while (!shell.isDisposed())
    {
        if (!display.readAndDispatch())
            display.sleep();
    }
    display.dispose();
}

private static boolean isEmpty(String input)
{
    if(input == null)
        return true;
    else
        return input.trim().isEmpty();
}

Looks like this:


The code will basically (on each key stroke) check if both Texts are empty. If so, disable the Button, else enable it.



来源:https://stackoverflow.com/questions/22245937/how-to-combine-and-validate-two-text-fields-for-a-swt-dialog

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!