Turns out there was both a JFX way to do this, plus EasyBind which is even more concise.

This commit is contained in:
Mike Hearn 2014-07-13 22:28:44 +02:00
parent 8068230042
commit 94968a491e
3 changed files with 7 additions and 20 deletions

View file

@ -46,6 +46,11 @@
<version>0.1</version>
</dependency>
-->
<dependency>
<groupId>org.fxmisc.easybind</groupId>
<artifactId>easybind</artifactId>
<version>1.0.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>de.jensd</groupId>
<artifactId>fontawesomefx</artifactId>

View file

@ -11,9 +11,9 @@ import javafx.scene.control.ProgressBar;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;
import javafx.util.Duration;
import org.fxmisc.easybind.EasyBind;
import wallettemplate.controls.ClickableBitcoinAddress;
import wallettemplate.utils.BitcoinUIModel;
import wallettemplate.utils.WTUtils;
import java.util.Date;
@ -42,7 +42,7 @@ public class Controller {
public void onBitcoinSetup() {
model = new BitcoinUIModel(bitcoin.wallet());
addressControl.addressProperty().bind(model.addressProperty());
balance.textProperty().bind(WTUtils.bindToString(model.balanceProperty(), Coin::toPlainString));
balance.textProperty().bind(EasyBind.map(model.balanceProperty(), Coin::toPlainString));
// Don't let the user click send money when the wallet is empty.
sendMoneyOutBtn.disableProperty().bind(model.balanceProperty().isEqualTo(Coin.ZERO));
}

View file

@ -1,12 +1,8 @@
package wallettemplate.utils;
import javafx.beans.binding.StringBinding;
import javafx.beans.value.ObservableValue;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.function.Function;
/**
* Some generic utilities to make Java a bit less annoying.
*/
@ -71,18 +67,4 @@ public class WTUtils {
return true;
}
}
// Why isn't this a part of the JFX Bindings class?
public static <T> StringBinding bindToString(ObservableValue<T> value, Function<T, String> function) {
return new StringBinding() {
{
super.bind(value);
}
@Override
protected String computeValue() {
return function.apply(value.getValue());
}
};
}
}