我在绑定Label
textProperty时遇到问题,我希望文本格式化,带有双变量。
如果我执行以下操作,它可以工作(标签文本属性已更新),但这样我就无法格式化文本。
label.textProperty().bind(model.doubleProperty().asString());
它不起作用(标签文本属性未更新):
StringBinding labelBinding = new StringBinding() {
{
bind(model.doubleProperty().asString());
}
@Override
protected String computeValue() {
if(model.getDouble() <= 0) {
return "---";
} else {
return df1.format(model.getDouble());
}
}
};
label.textProperty().bind(labelBinding);
谢谢你的帮助。
答案 0 :(得分:3)
我发现如何让它发挥作用,我真的不明白它是如何工作的。
StringBinding labelBinding = new StringBinding() {
{
bind(model.doubleProperty()); // don't call asString() here
}
@Override
protected String computeValue() {
if(model.getDouble() <= 0) {
return "---";
} else {
return df1.format(model.getDouble());
}
}
};
label.textProperty().bind(labelBinding);
但这会产生编译错误
label.textProperty().bind(model.doubleProperty());
答案 1 :(得分:0)
假设你有以下“JavaFX-Bean”:
import javafx.beans.property.DoubleProperty;
import javafx.beans.property.SimpleDoubleProperty;
import javafx.beans.property.SimpleStringProperty;
import javafx.beans.property.StringProperty;
public class Employee {
private final StringProperty lastname = new SimpleStringProperty();
private final StringProperty firstname = new SimpleStringProperty();
private final DoubleProperty salary = new SimpleDoubleProperty();
/**
* @return the lastname Property
*/
public StringProperty lastnameProperty() {
return lastname;
}
/**
* @return the lastname String
*/
public String getLastname() {
return lastname.get();
}
/**
* @param lastname the lastname to set
*/
public void setLastname(final String lastname) {
this.lastname.set(lastname);
}
/**
* @return the firstname property
*/
public StringProperty firstnameProperty() {
return firstname;
}
/**
* @return the firstname String
*/
public String getFirstname() {
return firstname.get();
}
/**
* @param firstname the firstname to set
*/
public void setFirstname(final String firstname) {
this.firstname.set(firstname);
}
/**
* @return the salary Property
*/
public DoubleProperty salaryProperty() {
return salary;
}
/**
* @return the salary double
*/
public double getSalary() {
return salary.get();
}
/**
* @param salary the salary to set
*/
public void setSalary(final double salary) {
this.salary.set(salary);
}
}
现在在应用程序的start方法中定义一个Label:
Label labaelSalary = new Label();
并将其与'JavaFX-Bean'的salaryProperty绑定:
labaelSalary.textProperty()
.bind(emp.salaryProperty().asString("%.2f"));
添加按钮并设置此按钮的操作:
Button changeSalaryButton = new Button("Change Salary");
changeSalaryButton.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(final ActionEvent event) {
double salary = Math.random() * 10000;
emp.setSalary(salary);
}
});
每次单击Button时,都会随机设置Employee对象emp的工资。 labelSalary的有界文本(属性)立即更改为显示emp Employee对象的salary属性的新值。 这是start方法的代码:
@覆盖
public void start(final Stage primaryStage) {
Employee emp = new Employee();
emp.setFirstname("Fred");
emp.setLastname("Las Frite");
emp.setSalary(12.500);
Label labelSalary = new Label();
labelSalary.textProperty()
.bind(emp.salaryProperty().asString("%.2f"));
Button changeSalaryButton = new Button("OK");
changeSalaryButton.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(final ActionEvent event) {
double salary = Math.random() * 10000;
emp.setSalary(salary);
}
});
HBox hBox = new HBox(10, labelSalary, changeSalaryButton);
hBox.setPadding(new Insets(20));
hBox.setAlignment(Pos.CENTER);
primaryStage.setScene(new Scene(hBox));
primaryStage.setWidth(400);
primaryStage.setHeight(600);
primaryStage.show();
}