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

Recommended Answers

Hi ,
Try requestFocus() method

Jump to Post

All 4 Replies

Member Avatar

You need to call setFocus from within an asyncExec call.

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

Hi ,
Try requestFocus() method

Member Avatar

requestFocus() works for AWT

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

Very good trick, Dale swift ! Thank you

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, learning, and sharing knowledge.