igorludi
igorludi

Reputation: 1581

How to make custom LineChart control in JavaFX?

I had a custom LineChartWithMarkers control in my JavaFX project. I've made a two year pause in my Java(FX) programming and found out that LineChartBuilder does not exist any more in Java 10. I was able to find some docs saying it is deprecated in version 8, but not how to replace it.

How do I fix my code to work with Java 10?

This is what I had:

public class LineChartWithMarkersBuilder extends LineChartBuilder {
    private Axis<Number> xAxis ;
    private Axis<Number> yAxis ;        
    private ObservableList<Series<Number,Number>> data ;

    public static LineChartWithMarkersBuilder create() {
        return new LineChartWithMarkersBuilder();
    }

    public LineChartWithMarkersBuilder xAxis(Axis<Number> xAxis) {
        this.xAxis = xAxis ;
        return this ;
    }

    public LineChartWithMarkersBuilder yAxis(Axis<Number> yAxis) {
        this.yAxis = yAxis ;
        return this ;
    }


    public LineChartWithMarkers<Number, Number> build() {
        xAxis = new NumberAxis();
        yAxis = new NumberAxis();
        return new LineChartWithMarkers<Number, Number>( xAxis, yAxis);

    }
}

and the custom LineChartWithMarkers:

public class LineChartWithMarkers<X extends Number, Y extends Number> extends LineChart<X, Y>  {

    private ObservableList<Data<Number, Number>> verticalMarkers;


    public LineChartWithMarkers(Axis<X> xAxis, Axis<Y> yAxis) {
        super(xAxis, yAxis);
        this.setCreateSymbols(false);
        verticalMarkers = FXCollections.observableArrayList(data -> new Observable[] {data.XValueProperty()});
        verticalMarkers.addListener((InvalidationListener)observable -> layoutPlotChildren());
    }
    (...)
}

which I used from .fxml like this:

...
                <LineChartWithMarkers fx:id="chartFit" createSymbols="false" layoutX="14.0" layoutY="54.0" prefHeight="499.0" prefWidth="987.0" AnchorPane.bottomAnchor="162.0" AnchorPane.leftAnchor="14.0" AnchorPane.rightAnchor="5.0" AnchorPane.topAnchor="54.0">
                <xAxis>
                    <NumberAxis side="BOTTOM" />
                </xAxis>
                <yAxis>
                    <NumberAxis side="LEFT" />
                </yAxis>
            </LineChartWithMarkers>
...

Upvotes: 0

Views: 131

Answers (1)

James_D
James_D

Reputation: 209299

All JavaFX builder classes were deprecated in Java 8 and removed in Java 9. Just remove the builder class entirely. To allow the FXMLLoader top instantiate a class without a no-argument constructor, use @NamedArg annotations on the constructor parameters:

public class LineChartWithMarkers<X extends Number, Y extends Number> extends LineChart<X, Y>  {

    private ObservableList<Data<Number, Number>> verticalMarkers;


    public LineChartWithMarkers(
            @NamedArg("xAxis") Axis<X> xAxis, 
            @NamedArg("yAxis") Axis<Y> yAxis) {
        super(xAxis, yAxis);
        this.setCreateSymbols(false);
        verticalMarkers = FXCollections.observableArrayList(data -> new Observable[] {data.XValueProperty()});
        verticalMarkers.addListener((InvalidationListener)observable -> layoutPlotChildren());
    }
    (...)
}

A complete discussion of the @NamedArg annotation can be found in What is the purpose of @NamedArg annotation in javaFX 8?

Upvotes: 1

Related Questions