JavaFX: Enlace StringProperty con el prefijo de cadena constante
Tengo una pregunta sobre la funcionalidad de enlace en JavaFX. Lo que quiero es enlazar 2 propiedades de cadena. Pero sus valores no deben ser iguales.
Vamos a hacer un ejemplo:
Tengo una StringProperty con representa el último proyecto abierto en mi aplicación.
El valor es como "C: \ temp \ myProject.prj".
Quiero mostrar esta ruta en el título de mi ventana.
Es fácil:stage.titleProperty().bind(lastprojectProperty());
Pero no quiero mostrar solo la ruta del proyecto sino también el nombre de la aplicación,
por ejemplo: MyApplication 2.2.4 - C: \ temp \ myProject.prj.
¿Es posible usar el enlace y agregar una cadena de prefijo constante? ¿O tengo un ChangeListerner?
La solución con el ChangeListener tiene el problema con los valores iniciales ...
final StringProperty path = new SimpleStringProperty("untitled");
final StringProperty title = new SimpleStringProperty("App 2.0.0");
path.addListener(new ChangeListener<String>()
{
@Override
public void changed(ObservableValue<? extends String> ov, String t, String newValue)
{
title.setValue("App 2.0.0 - " + newValue);
}
});
// My title shows "App 2.0.0" since there is now change event throws until now...
// Of course I could call path.setValue("untitled");
// And above path = new SimpleStringProperty("");
System.out.println(title.getValue());
// Now the title is correct: "App 2.0.0 - C:\temp\myProject.prj"
path.setValue("C:\\temp\\myProject.prj");
System.out.println(title.getValue());