scenebuilder javafx linechart

倾然丶 夕夏残阳落幕 提交于 2019-12-02 04:07:48

Don't create new objects for @FXML injected members

Never use new in conjunction with @FXML, i.e., never write:

@FXML 
private CategoryAxis xAxis = new CategoryAxis();

Instead, just write:

@FXML 
private CategoryAxis xAxis;

The FXMLLoader will automatically generate, i.e., create, a new object for each element in the FXML file and inject a reference to that into your controller where you provide an @FXML annotation. So if you reset the @FXML member reference to a new object you create in the controller there will be no association between that object and objects created by the loader.

Additionally, don't create another new LineChart within your initGraph() function. You already have a LineChart created by the FXMLLoader, just reference that. Same for NumberAxis and the other elements you are using @FXML injection with.

If you use an @FXML annotation also use <fx:id>

You have:

@FXML
private CategoryAxis xAxis;

So in your fxml, you should define:

<xAxis fx:id="xAxis">

Otherwise, the FXMLLoader will not be able to inject a reference to the axis you defined in your FXML.

Aside

You may have other errors in your code (e.g., around concurrency and threading). So the above might not be all of your errors. In general, when creating an mcve, try to eliminate anything which is not relevant to the question at hand (e.g. the threading code and non-linechart portions of the FXML), but include everything that somebody could use to copy and paste your code to compile and run it to replicate your issue.

Note: The Ensemble sample application contains a sample program which updates a graph in real-time based upon audio spectrum input data.

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