4
 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

fabian
  • 67,623
  • 12
  • 74
  • 102
dnjsdnwja
  • 337
  • 4
  • 13
  • 2
    Well I believe its indeed a bug with the update of the layout when you change the scenes. For some reason the state of the first container which holds the pie charts does not always update ( in my case, sometimes it update correctly ) so 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. This solve the bug for me. Now why exactly this is happening? I try to debug it but i could not find something, maybe someone can help you. – JKostikiadis Apr 16 '18 at 10:44

1 Answers1

0

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.

Wai Ha Lee
  • 7,664
  • 52
  • 54
  • 80
agolubev
  • 21
  • 2