dnjsdnwja
dnjsdnwja

Reputation: 409

Java9 bug in javafx PieChart labels

 import javafx.application.Application;
 import javafx.geometry.Pos;
 import javafx.scene.Scene;
 import javafx.scene.chart.PieChart;
 import javafx.scene.control.Button;
 import javafx.scene.layout.BorderPane;
 import javafx.scene.layout.VBox;
 import javafx.stage.Stage;
 import javafx.collections.FXCollections;
 import javafx.collections.ObservableList;

 public class Main extends Application {
@Override
public void start(Stage stage) {
    try {
        Scene pie;
        Scene begin;
        //pie scene
        ObservableList<PieChart.Data> pieChartData=FXCollections.observableArrayList();
        PieChart pieChart = new PieChart(pieChartData);
        Button btBack = new Button("Back");
        pieChart.setTitle("Test");
        VBox container = new VBox(20);
        container.getChildren().addAll(pieChart,btBack);
        container.setAlignment(Pos.CENTER);
        BorderPane pane = new BorderPane();
        pane.setCenter(container);
        pie =new Scene(pane,800,600);

        //begin scene
        VBox container2 = new VBox(20);
        Button btPie = new Button("pie");
        container2.getChildren().add(btPie);
        BorderPane pane2 = new BorderPane();
        container2.setAlignment(Pos.CENTER);
        pane2.setCenter(container2);
        begin=new Scene(pane2,50,50);


        //handler
        btPie.setOnAction(e->{
            pieChartData.clear();
            for(int i=0;i<5;++i)
            pieChartData.add(new PieChart.Data(""+i, i));
            stage.setScene(pie);
        });
        btBack.setOnAction(e->stage.setScene(begin));

        stage.setScene(begin);
        stage.show();

    } catch (Exception e) {

        e.printStackTrace(); // exception handling: print the error message on the console
    }
}

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

With the above code, it first shows the stage with a button "pie". Clicking the button shows a pie chart with a button "back". The back button is used to go back to the initial screen.

The problem in this code is that after showing the pie chart for the second time, the pie chart's labels suddenly become crammed.

It can be seen with

 1. click pie
 2. click back
 3. click pie -> problem shown

I can see that there is a problem, but I can't really see the reason. Furthermore, this problem only arise in java9; it works well in java8.

Can anyone find me the reason please?

enter image description here

Upvotes: 4

Views: 424

Answers (1)

agolubev
agolubev

Reputation: 31

The suggestion by JKostikiadis in the comment:

... you can fix the bug by adding container.layout(); after the for loop and before the stage.setScene(pie); in order to force the VBox to layout its children

helped me.

Upvotes: 1

Related Questions