Martin Dames
Martin Dames

Reputation: 264

How to bind subproperties of Objetcts in JavaFX

I know following code won't compile. But this is exactly what I want:

 public ObjectProperty<File> myFile = new SimpleObjectProperty<File>();
 Label fileName = new Label();
 fileName.textProperty().bind(myFile.getValue().getName());

Being able to bind properties of an object like java.util.File without declaring every property of File as e.g. StringProperty. How should I do this?

Upvotes: 1

Views: 621

Answers (1)

James_D
James_D

Reputation: 209438

In JavaFX 8, you can use the Bindings API to do this:

fileName.textProperty().bind(Bindings.selectString(myFile, "name"));

You can also consider using the EasyBind framework, with

fileName.textProperty().bind(EasyBind.map(myFile, File::getName));

Upvotes: 3

Related Questions