JavaFX 8 SceneBuilder LineChart data type


Question

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
1
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="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.

5
6/24/2014 12:08:45 AM

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