0

I want to reopen PieChart in PrimaryStage when I press the JButton in JFrame butit doesn't work when all scenes of PrimaryStage are closed.

Could you please help to restart pie chart in primary stage whenever I press the button?

...

    btnNewButton_1 = new JButton("check_1");
    btnNewButton_1.setBounds(14, 146, 180, 44);
    btnNewButton_1.addActionListener(this);
    getContentPane().add(btnNewButton_1);

    button_1 = new JButton("check_2");
    button_1.setBounds(14, 202, 180, 44);
    button_1.addActionListener(this);
    getContentPane().add(button_1);

...
@Override

public void actionPerformed(ActionEvent e) {
...
else if (e.getSource() == btnNewButton_1) {

        new JFXPanel();

        Platform.runLater(new Runnable() {
            @Override
            public void run() {
                try {
                    new AverageGender().start(new Stage());
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }
...
private class AverageGender extends Application {

    @Override
    public void start(Stage primaryStage) throws Exception {

        primaryStage.setTitle("check_1");
        Group root = new Group();

        ObservableList<PieChart.Data> pieChartData = FXCollections
                .observableArrayList(new PieChart.Data("Male", 20), new PieChart.Data("Female", 60));

        final PieChart pieChart = new PieChart(pieChartData);
        pieChart.setTitle("check_1");
        root.getChildren().add(pieChart);

        primaryStage.setScene(new Scene(root, 500, 400));

        primaryStage.setResizable(true);
        primaryStage.show();
    }
}
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
  • Need to clear `root.getChildren()` before adding new `pieChart` – bNd Dec 09 '16 at 08:30
  • it doesn't work neither... Is there anything to restart pieChart scene after I closed all screens of piechart?... root.getChildren().clear(); root.getChildren().add(pieChart); – PingaPenguin Dec 09 '16 at 08:38
  • 1) Please use code formatting for code and code snippets, structured documents like HTML/XML or input/output. To do that, select the text and click the `{}` button at the top of the message posting/editing form. 2) Java GUIs have to work on different OS', screen size, screen resolution etc. using different PLAFs in different locales. As such, they are not conducive to pixel perfect layout. Instead use layout managers, or [combinations of them](http://stackoverflow.com/a/5630271/418556) along with layout padding and borders for [white space](http://stackoverflow.com/a/17874718/418556). .. – Andrew Thompson Dec 09 '16 at 09:07
  • .. 3) For better help sooner, post a [MCVE] or [Short, Self Contained, Correct Example](http://www.sscce.org/). – Andrew Thompson Dec 09 '16 at 09:07
  • Don't re-create the view; update the model instead. If this is not a duplicate, please edit your question to include a [mcve] that shows your revised approach. Also, JFreeChart works in JavaFX directly; see `org.jfree.chart.fx.demo.PieChartFXDemo1`, for example. – trashgod Dec 09 '16 at 11:35

0 Answers0