Javafx:绑定== null时如何隐藏XYChart节点

时间:2019-04-02 15:49:07

标签: javafx binding linechart

我将一个折线图绑定到几个TextField,这些TextField包含双精度值字符串或为空。如果该字段包含一个数字,它可以正常工作,但是很不幸,当它为空时,我会收到一个异常。

我可以找到一种方法来处理“空”并将其设置为0.0,但是实际上我需要在这种情况下完全隐藏节点(如果“ X”或“ Y”字段为空)

有什么办法解决吗?

只有一组文本字段的基本示例:

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);
     }
}

1 个答案:

答案 0 :(得分:0)

我通过添加找到了解决方案

    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);
        }
    });

但是,如果有人知道更优雅的方式,我会很高兴的。