Question

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.

Was it helpful?

Solution

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:

enter image description here enter image description here


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

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top