Adjusting Height,Width of JavaFX Chart


Question

I'm new to Java with PHP, HTML, CSS experience. When I try to change the width and height my chart takes up in the window NetBeans gives me the error:

error: setWidth(double) has protected access in Region chart.setWidth(450);

I've searched through the javafx docs and found that width/height is bound to region, but I'm not sure what that is in my code, I tried a few things but haven't found it...

I'm sure this is simple..

thanks in advance, Brad.

package test;

import javafx.application.Application;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.stage.Stage;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.scene.chart.ScatterChart;
import javafx.scene.chart.XYChart;
import javafx.scene.chart.NumberAxis;
import javafx.scene.shape.Rectangle;

public class Test extends Application {



    private void init(Stage primaryStage) {
        Group root = new Group();
        primaryStage.setScene(new Scene(root,1000,1000));

        root.getStylesheets().add("test/Chart.css");

        Rectangle rect = new Rectangle(35,70); 
        rect.setLayoutX(30); 
        rect.setLayoutY(30);
        rect.getStyleClass().add("my-rect");

        NumberAxis xAxis = new NumberAxis("X Axis", -24d, 24.0d, 2.0d);
        NumberAxis yAxis = new NumberAxis("Y Axis", -24.0d, 24.0d, 1.0d);

        ObservableList<XYChart.Series> data = FXCollections.observableArrayList(
            new ScatterChart.Series("Series 1", FXCollections.<ScatterChart.Data>observableArrayList(
                new XYChart.Data(0.2, 3.5),
                new XYChart.Data(0.7, 4.6),
                new XYChart.Data(1.8, 1.7),
                new XYChart.Data(2.1, 2.8),
                new XYChart.Data(4.0, 2.2),
                new XYChart.Data(4.1, 2.6),
                new XYChart.Data(4.5, 2.0),
                new XYChart.Data(6.0, 3.0),
                new XYChart.Data(7.0, 2.0),
                new XYChart.Data(7.8, 4.0)
            )),
            new ScatterChart.Series("Series 2", FXCollections.<ScatterChart.Data>observableArrayList(
                new XYChart.Data(6.2,3.0),
                new XYChart.Data(6.0,4.0),
                new XYChart.Data(5.8,5.0)
            ))    
        );
        ScatterChart chart = new ScatterChart(xAxis, yAxis, data);
        chart.setWidth(450);
        chart.setHeight(450);

        chart.setLayoutX(250);
        chart.setLayoutY(250);



        root.getChildren().addAll(chart,rect);
    }

    @Override public void start(Stage primaryStage) throws Exception {
        init(primaryStage);
        primaryStage.show();
    }
    public static void main(String[] args) { launch(args); }
}
1
1
11/6/2012 5:40:13 AM

Accepted Answer

The javadoc of ScatterChart.getHeight() (which in turn is Region.getHeight()) says

Gets the value of the property height.
Property description:
The height of this resizable node. This property is set by the region's parent during layout and may not be set by the application. If an application needs to explicitly control the size of a region, it should override its preferred size range by setting the minHeight, prefHeight, and maxHeight properties.

Namely you can adjust and constraint the size of any chart with:

ScatterChart.setPrefHeight(double)
ScatterChart.setMinHeight(double)
ScatterChart.setMaxHeight(double)

ScatterChart.setPrefWidth(double)
ScatterChart.setMinWidth(double)
ScatterChart.setMaxWidth(double)

ScatterChart.setPrefSize(double, double)
ScatterChart.setMinSize(double, double)
ScatterChart.setMaxSize(double, double)
2
11/6/2012 11:19:42 AM

Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow
Icon