How to combine and check two text fields for swt dialog?

I have another question. I am using ModifyListener for a single text field to activate and deactivate the OK button in the swt dialog box. It works great.

Now I want to add a ModifyListener for another text field. I want the OK button to be activated only if there is one char in both min text fields.

This is the code for 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);
        }
    }
});

}

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 does not work because there are no dependencies between the two buttons. How can i combine it?

I want to set ok-button false while both modifylistener detect char. If I delete all characters in one test field, the button should be deactivated again.

Thank.

+4
1

Listener Text 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();
}

:

enter image description hereenter image description here


( ) , Text . , Button, .

+4

Source: https://habr.com/ru/post/1530562/


All Articles