JavaFX 8 SceneBuilder LineChart data type


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?

6/24/2014 12:11:27 AM

Accepted Answer

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="" xmlns:fx="" fx:id="xychart">
    <CategoryAxis fx:id="xAxis" side="BOTTOM" />
    <NumberAxis fx:id="yAxis" side="LEFT" />

Edit it to change the line:

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


<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.

6/24/2014 12:08:45 AM

Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow