The application uses fxml, contains two LineChart elements. I also created my successor from LineChart , but I can't figure out how to use it. It is impossible to create a custom element from it (although it may be that I am not doing so), it constantly writes an error:

Unable to instantiate.

Is it possible to create such elements from their classes? If so, how? Or maybe there is some other way in using fxml?

    1 answer 1

    In order for you to create a component, you must do the following:

    1. Inherit your class from one of the components (in your case from LineChart)

    2. In fxml add import with this class

    3. In the description of the element itself in fxml, you must describe the starting parameters - these are the parameters that you pass to the constructor.

    Example

     public class MyLineChart<X, Y> extends LineChart<X, Y> { String field1; Intger field2; public class MyLineChart(String field1, Integer field2, Axis<X> xAxis, Axis<Y> yAxis) { super(xAsix, yAxis); this.field1 = field1; this.field2 = field2; } 

    fxml

     <MyLineChart field1="SomeString" field2="100" xAsix="" yAxis=""/> 

    PS and generally create a class, do not include it in fxml, but simply manually add it to the panel (in the initialize method, create a new instance of your class, and add it where you need it)

    UPDATE

    Then do so if fxml does not work

     <BorderPane id="" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="232.0" prefWidth="289.0" xmlns:fx="http://javafx.com/fxml" fx:controller="charts2.Controller"> <top> <Pane id="pnHeader" prefHeight="67.0" prefWidth="790.0"> <children> <Label id="lblTitle" layoutX="14.0" layoutY="15.0" text="MyJavaFX"> <textFill> <Color blue="0.250" green="0.250" red="0.250" fx:id="x1" /> </textFill> </Label> </children> </Pane> </top> </BorderPane> 

    And in the class Controller.java write the following

     package charts2; import javafx.fxml.FXML; public class Controller { private LineMarkerChart lmc; @Ovveride public void initialize(URL url, ResourceBundle resourceBundle) { this.lmc = new LineMarketChart("asd",100,new Axis(), new Axis()); } } 
    • I tried to do as you advised - it didn’t work, or again I missed the link - Sidh
    • If you use the second method (create a class and connect it in the initialize method), then everything works - Sidh
    • see the modified answer - Andrew Bystrov