Skip to Main Content

Java SE (Java Platform, Standard Edition)

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

TextField setting the caret to the end when loosing focus

975244Nov 20 2012 — edited Nov 21 2012
Hey everyone,

I would like to set the caret of a TextField to the end of the text when the TextField loses the focus, so that the last part of the file name will be shwon and not the first part. I´ve tried the following:
ChangeListener<Boolean> onLooseFocus = new ChangeListener<Boolean>() 
    	{ 
    		@Override
    		public void changed(ObservableValue<? extends Boolean> observable, Boolean oldValue, Boolean newValue) 
    		{
    			if (!newValue.booleanValue()) 
    			{
    				textFieldDateiname.end();	
                                System.out.println(textFieldDateiname.getCaretPosition()); 
                                // At this point the caret is at the end and it will show:
                                //ReadOnlyIntegerProperty [bean: TextField[id=textFieldDateiname, styleClass=text-input text-field], name: caretPosition, value: 76]
    			}
    		}
    	};
this ChangeListener is added to the focusedProperty() method of my TextField
textFieldDateiname.focusedProperty().addListener(onLooseFocus);
It doesn´t work, although the caret is temporarily on the end of the text field.

Another question: If it works, I would like to reuse the ChangeListener onLooseFocus for diefferent TextField. So I would like to know which TextField lost the focus. I tried
observable.getSoruce(); // like it works with keyEvent
System.out.println(observable); // -> ReadOnlyBooleanProperty [bean: TextField[id=textFieldDateiname, styleClass=text-input text-field], name: focused, value: false]
So the id of the source of the ChangeEvent is known...

Any ideas?

Thanks,

Lukas

Edited by: 972241 on 20.11.2012 06:08
This post has been answered by James_D on Nov 20 2012
Jump to Answer
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Dec 19 2012
Added on Nov 20 2012
2 comments
2,685 views