JavaFX 8 SceneBuilder LineChart data type

不羁岁月 提交于 2019-12-11 06:21:28

问题


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())

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

What am I missing here?


回答1:


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>

Edit it to change the line:

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

to:

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

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.



来源:https://stackoverflow.com/questions/24375047/javafx-8-scenebuilder-linechart-data-type

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!