hi!

First of all sorry if my english is a little poor

I want evaluate a text box named txt1, the focus shouldn't exit from the text box until the user write any in it.

This is my code:

txt1.addFocusListener(new FocusListener() {

    		public void focusLost(FocusEvent e) {

    			if (((Text) e.getSource()).getText().isEmpty())
    			{
    				System.out.println("txt1 is empty");
    				((Text) e.getSource()).setFocus();
    			}
    		}

    		public void focusGained(FocusEvent e) {
    		}
    	});

when the txt1 lost the focus and is empty the message "txt1 is empty" is viewed on the console but the focus is not seted to txt1. Any who can help me?

thanks

Member Avatar

dale.swift

You need to call setFocus from within an asyncExec call.

Display.getCurrent().asyncExec(new Runnable() {
	public void run() {
		errorField.setFocus();
	}
});
Member Avatar

dale.swift

requestFocus() works for AWT

Otherwise, for SWT, you need to callSetFocus() from asyncExec()

Very good trick, Dale swift ! Thank you