Lunigorn
Lunigorn

Reputation: 1412

JavaFX 2.2: How to set SimpleBooleanProperty value to null?

If I do so:

BooleanProperty b = new SimpleBooleanProperty();
b.setValue(null);
System.out.println(b.getValue());

I receive output:

false

How to set SimpleBooleanProperty value to null? Set SimpleBooleanProperty to null (BooleanProperty b = null;) is bad idea, because I will use binding.

I founded the way:

ObjectProperty<Boolean> b = new SimpleObjectProperty<Boolean>(null);
System.out.println(b.getValue());

Works fine.

I can't answer on mine questions, so I put it here, sorry.

Upvotes: 2

Views: 4457

Answers (2)

assylias
assylias

Reputation: 328618

SimpleBooleanProperty is a wrapper around a boolean (primitive) - null values are automatically set to the default (false) value.

If you want to allow null values, you can use an ObjectProperty<Boolean> b = new SimpleObjectProperty<> ();. The drawback is that you lose the default boolean bindings.

Alternatively, you could create a custom class that overrides the existing setValue implementation, but that could prove somewhat complex because it relies on the set(boolean) method which obviously can't accept null...

Upvotes: 6

mre
mre

Reputation: 44240

I don't think there's a way to set the value to null. Look at the BooleanProperty#setValue implementation,

public void setValue(Boolean paramBoolean)
{
    set(paramBoolean == null ? false : paramBoolean.booleanValue());
}

This is exactly the behavior you're seeing.

Upvotes: 1

Related Questions