【问题标题】:Binding Chart to TableView JavaFX将图表绑定到 TableView JavaFX
【发布时间】:2023-03-12 21:32:01
【问题描述】:

我有一个包含数据的 TableView,并且想要一个 PieChart 来可视化该表中的数据。我不明白我应该如何将数组列表从 TableView 绑定到 Piechart,因为 TableView 需要 getter 和 setter,而 piechart 需要 PieChart.Data 可观察列表。

这是我目前的代码,元素是用 Scene Builder 设计的

@FXML
private TableView<Record> tableView;



static ObservableList<Record> dataen
        = FXCollections.observableArrayList(
                new Record("January", 100),
                new Record("February", 200));




public void initialize(URL url, ResourceBundle rb) {

    Month.setCellValueFactory(new PropertyValueFactory("fieldMonth"));
    Value.setCellValueFactory(new PropertyValueFactory("fieldValue"));
    tableView.setItems(dataen);

}

}

【问题讨论】:

  • Oracle sample 在桌子上使用监听器。
  • 我见过这个,但在我的例子中它根本没有帮助我。我正在使用场景生成器,问题是我无法将可观察列表“dataen”设置为 PieChart..

标签: javafx


【解决方案1】:

要么在您的 TableView 项目列表 (dataen) 上创建一个侦听器,要么对两者使用相同的列表。在这里,他们使用相同的列表。您可以将列表类型更改为 &lt;PieChart.Data&gt; 而不是 &lt;Record&gt;,否则请使用侦听器。

import javafx.application.Application;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.scene.Scene;
import javafx.scene.chart.PieChart;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.scene.control.cell.TextFieldTableCell;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
import javafx.util.converter.NumberStringConverter;

public class ChangePie extends Application {

    @Override
    public void start(Stage primaryStage) {
        ObservableList<PieChart.Data> pieChartData =
                FXCollections.observableArrayList(
                new PieChart.Data("Grapefruit", 13),
                new PieChart.Data("Oranges", 25),
                new PieChart.Data("Plums", 10),
                new PieChart.Data("Pears", 22),
                new PieChart.Data("Apples", 30));
        final PieChart chart = new PieChart(pieChartData);
        chart.setTitle("Imported Fruits");

        TableView tv = new TableView(pieChartData);
        tv.setEditable(true);

        TableColumn tc1 = new TableColumn("Name");
        tc1.setPrefWidth(100);
        tc1.setCellValueFactory(new PropertyValueFactory("name"));
        tc1.setCellFactory(TextFieldTableCell.forTableColumn());

        TableColumn tc2 = new TableColumn("Data");
        tc2.setPrefWidth(100);
        tc2.setCellValueFactory(new PropertyValueFactory("pieValue"));
        tc2.setCellFactory(TextFieldTableCell.forTableColumn(new NumberStringConverter()));

        tv.getColumns().addAll(tc1,tc2);
        Scene scene = new Scene(new VBox(chart,tv));
        primaryStage.setScene(scene);
        primaryStage.show();
    }

    public static void main(String[] args) {
        launch(args);
    }

}

【讨论】:

  • 谢谢。但是当我使用scenebuilder时我该怎么办?我有 "Month.setCellValueFactory(new PropertyValueFactory("fieldMonth")); Value.setCellValueFactory(new PropertyValueFactory("fieldValue")); tableView.setItems(dataen);"但是当我从 Record 更改为 PieChart 数据时,tableview 没有得到任何数据..
  • 哇,谢谢。找到了,不得不改:Month.setCellValueFactory(new PropertyValueFactory("fieldMonth"));改为“name”而不是“fieldMonth”,怎么回事?
  • PieChart.Data 已经为其字段、名称和 pieValue 命名。如果您只是使用这些字段,那么您不需要单独的表数据类。
  • 谢谢,pieValue 和 pieName 是饼图的标准吗?
猜你喜欢
  • 2012-08-28
  • 2012-05-28
  • 2013-09-08
  • 1970-01-01
  • 2014-08-06
  • 1970-01-01
  • 2017-04-11
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多