-1

I am in the process of moving my JavaFX project from Java 8 to Java 11, JavaFX 17 for the past week or so. I also moved from Scenebuilder 8.5 to Scenebuilder JavaFX Version: 20.0.1. All in all it has been reasonably smooth, however I am facing a confusing error that I cannot seem to solve.

Instances of `javafx.scene.chart.LineChart` cannot be created by FXML loader.

When I changed the Java version of my project to Java 11, and imported the JavaFX 17 library to my project, The LineChart I am using in my project began throwing an error I had never seen before in the FXML Document file. It is a 330 line file so I will only include the relevant parts:

<?import javafx.scene.chart.CategoryAxis?>
<?import javafx.scene.chart.LineChart?>
<?import javafx.scene.chart.NumberAxis?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.ChoiceBox?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.control.TableView?>
<?import javafx.scene.control.TextField?>
<?import javafx.scene.effect.DropShadow?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.layout.HBox?>
<?import javafx.scene.layout.Pane?>
<?import javafx.scene.layout.VBox?>
<?import javafx.scene.shape.Line?>


<LineChart fx:id="chartDashboardLineChart" layoutX="5.0" layoutY="10.0" prefHeight="233.0" prefWidth="870.0" title="Monthly Sales">
   <xAxis>
     <CategoryAxis side="BOTTOM" fx:id="x" />
   </xAxis>
   <yAxis>
     <NumberAxis fx:id="y" side="LEFT" />
   </yAxis>
</LineChart>

The error is displayed under the line where the LineChart chartDashboardLineChart is declared. There are also two new errors which read: Class javafx.scene.chart.LineChart does not support property xAxis (and the same for yAxis). These two errors seem less worrying to me than the FXML Loader error above. I will also include the relevant lines of code from my project, although I believe it is more likely that the error lies within the FXML Document. Here is the Java Code (this is split across 2 different classes: Class: LineCharts.java

And another class, this one too large to post in its entirety called DashboardController.java, Here are the relevant parts of the DashboardController class:

@FXML
private LineChart<?, ?> chartDashboardLineChart;
@FXML
private CategoryAxis x;
@FXML
private NumberAxis y;
LineCharts LC = new LineCharts();

//later on in the initialize area of the code

//Call the initializeLineChart method and pass the line chart as an argument
LC.generateDashboardLineChart(chartDashboardLineChart);

I have tried numerous things to try and fix this issue, but with no success. It seems it is a rare issue to have as there is little to no information that I could find online.

I appreciate any bit of help or any theories as to what is causing my issue, and how to solve it. Thank you!

trashgod
  • 203,806
  • 29
  • 246
  • 1,045
amk Fitz
  • 1
  • 2
  • 1
    Can you post the complete stack trace? Also, why are you using wildcards in the declaration of the line chart in the controller? – James_D Aug 22 '23 at 17:43
  • And, I suppose, clarify exactly where and when these errors occur. The error in the title is a runtime error generated when you try and run the application? Or when you try to run SceneBuilder? The "properties not supported" errors are "in the FXML file"? Meaning they just show up in the IDE? – James_D Aug 22 '23 at 17:59
  • 1
    Please do not post screenshots of code. – jewelsea Aug 22 '23 at 18:31

3 Answers3

1

I can't replicate this either by running the app or in SceneBuilder.

Verify that the following code works in your environment.

Test Environment

Execution Environment:

OS X 13.4.1, JavaFX 20.0.2, OpenJDK 19.0.2

SceneBuilder Version (from the About screen):

Product Version
JavaFX Scene Builder 19.0.0

Build Information
Version 19.0.0
Date: 2022-10-07 07:42:50
JavaFX Version: 19
Java Version: 17, OpenJDK Runtime Environment

Test Application

line-chart.fxml

<?import javafx.scene.chart.CategoryAxis?>
<?import javafx.scene.chart.LineChart?>
<?import javafx.scene.chart.NumberAxis?>

<LineChart fx:controller="com.test.demo.chart.ChartController"
           xmlns="http://javafx.com/javafx" 
           xmlns:fx="http://javafx.com/fxml" 
           fx:id="chartDashboardLineChart" 
           layoutX="5.0" layoutY="10.0" 
           prefHeight="233.0" prefWidth="870.0" 
           title="Monthly Sales">
    <xAxis>
        <CategoryAxis side="BOTTOM" fx:id="x" />
    </xAxis>
    <yAxis>
        <NumberAxis fx:id="y" side="LEFT" />
    </yAxis>
</LineChart>

ChartController.java

package com.test.demo.chart;

import javafx.scene.chart.CategoryAxis;
import javafx.scene.chart.LineChart;
import javafx.scene.chart.NumberAxis;

public class ChartController {
    public LineChart<String, Double> chartDashboardLineChart;
    public CategoryAxis x;
    public NumberAxis y;
}

LineChartApp.java

package com.test.demo.chart;

import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Scene;
import javafx.stage.Stage;

public class LineChartApp extends Application {
    @Override
    public void start(Stage stage) throws Exception {
        FXMLLoader loader = new FXMLLoader(
                LineChartApp.class.getResource(
                        "line-chart.fxml"
                )
        );

        stage.setScene(
                new Scene(
                        loader.load()
                )
        );
        stage.show();
    }

    public static void main(String[] args) {
        Application.launch(args);
    }
}

Output

monthly sales

jewelsea
  • 150,031
  • 14
  • 366
  • 406
1

I can reproduce this finding using MacOS 12.6, JDK 17.0.8, SceneBuilder 19, this Status Application or this Test Application, and NetBeans 18 with the JavaFX 2 v1.37 plugin mentioned here and here. As the plugin is outmoded, deactivating it is a simple workaround. Doing so eliminates the (spurious) error and warnings.

Plugins

With reference to line-chart.fxml cite above:

 5: Instances of `javafx.scene.chart.LineChart` cannot be created by FXML loader.
12: Class `javafx.scene.chart.LineChart` does not support property `xAxis`.
15: Class `javafx.scene.chart.LineChart` does not support property `yAxis`.
trashgod
  • 203,806
  • 29
  • 246
  • 1,045
0

This is a very strange error; while the error still shows on netbeans (13) in the FXML Document, it seems that when I run the project the line chart works regardless. Here is a screenshot showing the errors on netbeans:

Errors Screenshot

Here is an image showing that it runs and displays the line chart regardless:!

LineChart in action

Thanks to everyone who posted a suggestion, I very much appreciate it. I still do not understand why errors are being displayed in the FXML Document, but ultimately it does not seem to be affecting my project.

trashgod
  • 203,806
  • 29
  • 246
  • 1,045
amk Fitz
  • 1
  • 2
  • 1
    I suspect the plugin mentioned illustrated [here](https://stackoverflow.com/a/76963655/230513) and re-tagged your question; please don't hesitate to edit your question accordingly; I'd welcome learning of your result. – trashgod Aug 24 '23 at 16:59
  • Going forward, you can accept an answer by clicking on the [empty check mark](http://meta.stackoverflow.com/a/5235/163188) at the answer's left. – trashgod Aug 24 '23 at 17:00