Does JavaFX lack a public Property.getObservable() method?
Randahl Fink Isaksen
randahl at rockit.dk
Fri Sep 12 16:50:56 UTC 2014
Thank you both of you for some good tips. I will try those approached instead.
Randahl
On 12 Sep 2014, at 14:37, Tomas Mikula <tomas.mikula at gmail.com> wrote:
> Hi Randahl,
>
> if on button click you only want to update an external boolean
> property, bidirectional binding suggested by Martin should be all you
> need.
>
> If the problem is more complex, it might be useful to recognize that
> your toggle button serves two distinct functions:
> 1. It reflects the state (on/off) of an external object.
> 2. It manipulates an external object.
>
> Treat these functions separately, i.e. on button click, don't change
> the button value (and thus appearance) directly, but manipulate the
> observed object.
>
> // observe external object
> button.valueProperty().bind(externalObject.stateProperty());
>
> // manipulate external object
> button.setOnAction(event -> {
> if(button.getValue()) {
> externalObject.turnOff();
> } else {
> externalOBject.turnOn();
> }
> });
>
> Your original button behavior of just changing the value (and thus
> appearance) is achieved by
>
> button.setOnAction(event -> button.setValue(!button.getValue()));
>
> Best,
> Tomas
>
> On Fri, Sep 12, 2014 at 1:08 PM, Martin Sladecek
> <martin.sladecek at oracle.com> wrote:
>> Hi Randahl,
>> why don't you use bidirectional binding for this purpose?
>>
>> -Martin
>>
>>
>> On 09/12/2014 01:04 PM, Randahl Fink Isaksen wrote:
>>>
>>> I have noticed the lack of a getObservable() method of the property class,
>>> and I have come across a use case which might justify such a method, so I
>>> would like to discuss whether posting a new Jira issue for this is
>>> justified.
>>>
>>> I have implemented a simple toggle button with two states, on and off. The
>>> toggle button has a SimpleBooleanProperty value, and when the user switches
>>> the toggle button the value is negated.
>>>
>>> Now, in some cases I would like to bind the valueProperty to some
>>> observable boolean in my app, so that whenever that observable boolean is
>>> true, the button reflects that by switching to on. However, the problem is
>>> that if I bind the SimpleObjectProperty value to an observable boolean
>>> value, and then click the button to toggle it, a JavaFX exception tells me
>>> that I cannot negate the boolean value, since it is now bound.
>>>
>>> In such cases, I think it makes sense to have my toggle button change the
>>> state of the value it is bound to instead. But since the
>>> SimpleBooleanProperty does not have a getObservable() method, I have no way
>>> of accessing the value observed.
>>>
>>> Does this justify adding a public getObservable method to class Property?
>>>
>>> Yours
>>>
>>> Randahl
>>>
>>>
>>>
>>
Randahl Fink Isaksen
Inventor / Software architect / Java expert
Owner of ROCK IT
Mobile +45 26 25 88 84
Skype randahl
Twitter @r4nd4hl
More information about the openjfx-dev
mailing list