【问题标题】:Javafx: How to hide XYChart Node when binding == nullJavafx:绑定== null时如何隐藏XYChart节点
【发布时间】:2019-04-02 15:49:07
【问题描述】:

我正在将折线图绑定到几个 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);
     }
}

【问题讨论】:

    标签: javafx binding linechart


    【解决方案1】:

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

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

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

    【讨论】:

      猜你喜欢
      • 2019-03-18
      • 1970-01-01
      • 2014-05-01
      • 1970-01-01
      • 1970-01-01
      • 2011-06-01
      • 1970-01-01
      • 1970-01-01
      • 2012-05-12
      相关资源
      最近更新 更多