Javafx:绑定==null时如何隐藏XYChart节点
我正在将一个折线图绑定到几个文本字段,这些文本字段包含双值字符串或为空。如果该字段包含一个数字,它可以正常工作,但不幸的是,当它为空时,我会得到一个异常 我可以找到一种方法来处理“Emptynes”并将其设置为0.0,但实际上在这种情况下我需要完全隐藏节点(如果“X”或“Y”字段为空) 有什么办法解决吗 仅包含一组文本字段的基本示例:Javafx:绑定==null时如何隐藏XYChart节点,javafx,binding,linechart,Javafx,Binding,Linechart,我正在将一个折线图绑定到几个文本字段,这些文本字段包含双值字符串或为空。如果该字段包含一个数字,它可以正常工作,但不幸的是,当它为空时,我会得到一个异常 我可以找到一种方法来处理“Emptynes”并将其设置为0.0,但实际上在这种情况下我需要完全隐藏节点(如果“X”或“Y”字段为空) 有什么办法解决吗 仅包含一组文本字段的基本示例: import javafx.application.Application; import javafx.beans.binding.Bindings; impo
import javafx.application.Application;
import javafx.beans.binding.Bindings;
import javafx.scene.Scene;
import javafx.scene.chart.LineChart;
import javafx.scene.chart.NumberAxis;
import javafx.scene.chart.XYChart;
import javafx.scene.chart.XYChart.Data;
import javafx.scene.control.TextField;
import javafx.scene.layout.AnchorPane;
import javafx.stage.Stage;
public class LineChartSample extends Application {
@Override
public void start(Stage stage) {
stage.setTitle("Demo");
final NumberAxis xAxis = new NumberAxis();
final NumberAxis yAxis = new NumberAxis();
xAxis.setLabel("X");
yAxis.setLabel("Y");
final LineChart<Number, Number> lineChart = new LineChart<Number, Number>(xAxis, yAxis);
AnchorPane ap = new AnchorPane();
lineChart.setTitle("Demo");
XYChart.Series series = new XYChart.Series();
AnchorPane.setTopAnchor(lineChart, 5d);
AnchorPane.setLeftAnchor(lineChart, 5d);
AnchorPane.setRightAnchor(lineChart, 5d);
TextField t1 = new TextField("33.3");
TextField t2 = new TextField("33.3");
Data d = new XYChart.Data();
d.XValueProperty().bind(Bindings.when(t1.textProperty().isEmpty())
.then(0.0) // <-- here is the problem
.otherwise(Bindings.createDoubleBinding(() -> {
return Double.parseDouble(t1.getText());
}, t1.textProperty())));
d.YValueProperty().bind(Bindings.when(t2.textProperty().isEmpty())
.then(0.0) // <-- here is the problem
.otherwise(Bindings.createDoubleBinding(() -> {
return Double.parseDouble(t2.getText());
}, t2.textProperty())));
series.getData().add(d);
AnchorPane.setBottomAnchor(t1, 50d);
AnchorPane.setLeftAnchor(t1, 5d);
AnchorPane.setBottomAnchor(t2, 50d);
AnchorPane.setRightAnchor(t2, 5d);
ap.getChildren().addAll(lineChart, t1, t2);
Scene scene = new Scene(ap, 800, 600);
lineChart.getData().add(series);
stage.setScene(scene);
stage.show();
}
public static void main(String[] args) {
launch(args);
}
}
导入javafx.application.application;
导入javafx.beans.binding.Bindings;
导入javafx.scene.scene;
导入javafx.scene.chart.LineChart;
导入javafx.scene.chart.NumberAxis;
导入javafx.scene.chart.XYChart;
导入javafx.scene.chart.XYChart.Data;
导入javafx.scene.control.TextField;
导入javafx.scene.layout.ancorpane;
导入javafx.stage.stage;
公共类LineChartSample扩展了应用程序{
@凌驾
公众假期开始(阶段){
舞台。片名(“演示”);
最终数字axis xAxis=新数字axis();
最终数字axis yAxis=新数字axis();
xAxis.setLabel(“X”);
yAxis.setLabel(“Y”);
最终线形图线形图=新线形图(xAxis,yAxis);
AnchorPane ap=新的AnchorPane();
线形图。设置标题(“演示”);
XYChart.Series系列=新的XYChart.Series();
AnchorPane.setTopAnchor(线形图,5d);
AnchorPane.setLeftAnchor(线形图,5d);
AnchorPane.setRightAnchor(线形图,5d);
TextField t1=新的TextField(“33.3”);
TextField t2=新的TextField(“33.3”);
数据d=新的XYChart.Data();
d、 XValueProperty().bind(Bindings.when(t1.textProperty().isEmpty())
.然后(0.0)//{
返回Double.parseDouble(t1.getText());
},t1.textProperty());
d、 YValueProperty().bind(Bindings.when(t2.textProperty().isEmpty())
.然后(0.0)//{
返回Double.parseDouble(t2.getText());
},t2.textProperty());
series.getData().add(d);
锚烷立根摇床(t1,50d);
AnchorPane.setLeftAnchor(t1,5d);
锚烷立根摇床(t2,50d);
AnchorPane.setRightAnchor(t2,5d);
ap.getChildren().addAll(折线图,t1,t2);
场景=新场景(美联社,800600);
lineChart.getData().add(系列);
舞台场景;
stage.show();
}
公共静态void main(字符串[]args){
发射(args);
}
}
我通过添加
t1.textProperty().addListener((observable) -> {
if (t1.getText().isEmpty() || t2.getText().isEmpty()) {
d.getNode().setVisible(false);
} else {
d.getNode().setVisible(true);
}
});
t2.textProperty().addListener((observable) -> {
if (t1.getText().isEmpty() || t2.getText().isEmpty()) {
d.getNode().setVisible(false);
} else {
d.getNode().setVisible(true);
}
});
但如果有人知道一种更优雅的方式,我会非常高兴。我通过添加
t1.textProperty().addListener((observable) -> {
if (t1.getText().isEmpty() || t2.getText().isEmpty()) {
d.getNode().setVisible(false);
} else {
d.getNode().setVisible(true);
}
});
t2.textProperty().addListener((observable) -> {
if (t1.getText().isEmpty() || t2.getText().isEmpty()) {
d.getNode().setVisible(false);
} else {
d.getNode().setVisible(true);
}
});
但如果有人知道更优雅的方式,我会很高兴