本文介绍了JavaFX 8 SceneBuilder LineChart数据类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

似乎在SceneBuilder中创建LineChart时,它默认为String。那就是: LineChart< String,String>

It seems like when I create a LineChart in SceneBuilder it defaults to "String". That is: LineChart<String, String>.

当我在代码中加载它然后尝试添加一些数据我得到:

When I load it in code then try to add some data I get:

Exception in thread "JavaFX Application Thread" java.lang.ClassCastException: java.lang.Double cannot be cast to java.lang.String

如果我在代码中创建图表,它可以正常工作:

It works fine if I create the chart in code instead:

val chart = new LineChart[Number, Number]( new NumberAxis(), new NumberAxis())

我在SceneBuilder中看不到任何允许我选择数字作为图表数据类型的选项。

I don't see any option in SceneBuilder that allows me to choose "Number" as the type of data the chart will have.

我在这里缺少什么?

推荐答案

只需编辑SceneBuilder生成的FXML例如,在文本编辑器中,如果在SceneBuilder 2中创建一个新的LineChart,它将生成以下FXML:

Just edit the FXML generated by SceneBuilder in a text editor, for example, if you create a new LineChart in SceneBuilder 2, it will generate the following FXML:

<?xml version="1.0" encoding="UTF-8"?>

<?import java.lang.*?>
<?import javafx.scene.chart.*?>

<LineChart xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:id="xychart">
  <xAxis>
    <CategoryAxis fx:id="xAxis" side="BOTTOM" />
  </xAxis>
  <yAxis>
    <NumberAxis fx:id="yAxis" side="LEFT" />
  </yAxis>
</LineChart>

编辑它以更改行:

<CategoryAxis fx:id="xAxis" side="BOTTOM" />

to:

<NumberAxis fx:id="xAxis" side="BOTTOM" />

图表将自动在SceneBuilder中重新加载(因为它会监视对FXML文件的编辑)以包含两个数字轴而不是NumberAxis和CategoryAxis。

The chart will automatically reload in SceneBuilder (because it watches for edits to the FXML file) to include two number axes instead of a NumberAxis and a CategoryAxis.

这篇关于JavaFX 8 SceneBuilder LineChart数据类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-27 00:33