0

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

4
Contributors
4
Replies
5
Views
10 Years
Discussion Span
Last Post by malokran
0

You need to call setFocus from within an asyncExec call.

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

requestFocus() works for AWT

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

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.