focus property in composed control

Tom Eugelink tbee at tbee.org
Mon Feb 23 09:54:25 UTC 2015


It actually is fairly simple. This is what ideally should happen:

CalendarTextField extends Control
- Node has: ReadOnlyBooleanProperty focusProperty();

CalendarTextFieldSkin extends Skin
- private TextField textField = new TextField();
- *getSkinnable().focusProperty().bind(textField.focusProperty())*



But this is not allowed, so I got it working like this:

CalendarTextFieldSkin extends Skin
- private TextField textField = new TextField();
- public BooleanProperty focusForward = new SimpleBooleanProperty();
-       textField.focusedProperty().addListener( (observable) -> {
             focusForward.set(textField.focusedProperty().get());
         });

CalendarTextField extends Control
- constructor:
         skinProperty().addListener( (observable) -> {
             Skin<?> skin = getSkin();
             if (skin instanceof CalendarTextFieldSkin) {
                 CalendarTextFieldSkin lCalendarTextFieldSkin = (CalendarTextFieldSkin)skin;
                 lCalendarTextFieldSkin.focusForward.addListener( (observable2) -> {
*super.setFocused(lCalendarTextFieldSkin.focusForward.get());*
                 });
             }
         });

https://github.com/JFXtras/jfxtras/blob/8.0/jfxtras-controls/src/main/java/jfxtras/internal/scene/control/skin/CalendarTextFieldSkin.java
line 146 and 209

https://github.com/JFXtras/jfxtras/blob/8.0/jfxtras-controls/src/main/java/jfxtras/scene/control/CalendarTextField.java
line 90




On 23-2-2015 09:33, Michael Heinrichs wrote:
> Hi Tom,
>
> can you provide a code example? I am not sure I understand all the details correctly. :) In particular it is important to know which of the properties are defined in your code and which properties you are just using. For example it is possible to bind a readonly property, but only if it is defined in your code.
>
> Cheers,
> Michael
>
>
>> On 23 Feb 2015, at 09:03, Tom Eugelink <tbee at tbee.org> wrote:
>>
>> JFXtras has a number of extended textfields (BigDecimal, Calendar, LocalDate, ...). These controls use a TextField in their skin to compose this control. These extended textfield controls have a readonly focusProperty()... What would be the best way to forward the focusProperty of the TextField to the control's focusProperty?
>> - Binding is not possible, because it is read only.
>> - Setting the value in the skin is not possible, because it is read only.
>> - setFocused method is protected final
>>
>> What works is the following setup:
>> - create a _public_ focusForwardProperty in the Skin
>> - listen to the focusProperty of TextField and set the focusForwardProperty accordingly
>> - listen to the skinProperty in the Control and when set, bind a listener to the focusForwardProperty
>> - in this  listener call setFocused with the value of focusForwardProperty
>>
>> This approach at least prevents the control's API to be polluted with public methods, but requires a property just for the sake of publishing the value. Ideally one would like to bind both focusProperties.
>>
>> Tom



More information about the openjfx-dev mailing list