SpaceMonkey
SpaceMonkey

Reputation: 4365

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?

Upvotes: 1

Views: 4846

Answers (1)

jewelsea
jewelsea

Reputation: 159406

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.

Upvotes: 6

Related Questions