0

First, I apologize for the English ... lol

The first time you run the process to generate the chart, runs smoothly, but the second time on, the error is displayed: "java.lang.IllegalStateException: Application launch must not be called more than once"

Help me, I do not know what to do to work.

Application code:

package reports;

import java.io.File;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.List;

import javax.imageio.ImageIO;

import javafx.application.Application;
import javafx.application.Platform;
import javafx.embed.swing.SwingFXUtils;
import javafx.scene.Scene;
import javafx.scene.SnapshotParameters;
import javafx.scene.chart.BarChart;
import javafx.scene.chart.CategoryAxis;
import javafx.scene.chart.NumberAxis;
import javafx.scene.chart.XYChart;
import javafx.scene.image.WritableImage;
import javafx.stage.Stage;
import models.dao.ReportDAO;
import models.dto.ReportHarvestersTimeManagmentDTO;

public class ReportPerfCompHarvTime extends Application {

    private ReportDAO reportDAO = new ReportDAO();
    private String folder;
    private String chartName;

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

    public void run(String pFolder, String pChartName) throws Throwable {
        this.folder = pFolder;
        this.chartName = pChartName;
        launch();
    }

    @SuppressWarnings({ "unchecked", "rawtypes" })
    @Override
    public void start (Stage stage) {
        try {
            // Stage stg = new Stage();
            // stage.setTitle("GESTÃO DO TEMPO (%)");

            Platform.setImplicitExit(false);

            NumberAxis xAxis = new NumberAxis();
            CategoryAxis yAxis = new CategoryAxis();
            BarChart<Number, String> bc = new BarChart<Number, String>(xAxis, yAxis);

            // bc.setTitle("GESTÃO DO TEMPO (%)");
            xAxis.setTickLabelRotation(90);
            yAxis.setLabel("Frente");

            List<ReportHarvestersTimeManagmentDTO> listReport = reportDAO.lstReportHarvestersTimeManagmentDTO();

            BigDecimal value = new BigDecimal(0);
            BigDecimal value100 = new BigDecimal(100);
            XYChart.Series series = new XYChart.Series();

            for (ReportHarvestersTimeManagmentDTO lst : listReport) {
                series.setName(lst.getActivityName());
                if (lst.getWorkedHours().compareTo(BigDecimal.ZERO) > 0) {
                    value = new BigDecimal(0);
                    value = lst.getTimeManagmentHours().divide(lst.getWorkedHours(), 2, RoundingMode.HALF_UP);
                    value = value.multiply(value100);
                    series.getData().add(new XYChart.Data(value.doubleValue(), lst.getWorkfrontID().toString()));
                }
            }

            bc.getData().add(series);

            bc.setAnimated(false);
            bc.applyCss();
            bc.layout();

            Scene scene = new Scene(bc, 800, 600);
            stage.setScene(scene);
            stage.show();

            WritableImage image = bc.snapshot(new SnapshotParameters(), null);
            File file = new File(this.folder + this.chartName + ".png");
            ImageIO.write(SwingFXUtils.fromFXImage(image, null), "png", file);

        } catch (Exception e) {
            e.printStackTrace();
        } catch (Throwable e) {
            e.printStackTrace();
        } finally {
            stage.close();
            Platform.exit();
        }
    }

}
  • What do you mean by "the second time on"? There is no second time on. You only enter your application once via its main method. You certainly shouldn't be calling main again, or calling launch directly in any other sense. – ManoDestra Aug 01 '16 at 20:58
  • @ManoDestra ... My application is web, and in it, I have a button to generate the report. When I click the first time, the report generates ok, but if I click the button again, the error is displayed ... the error is displayed when run the "launch(args)" again – Mateus Barcelini Aug 02 '16 at 13:13
  • Don't run it again. You're only meant to run it once on launch. You can't call it again. End your application first, then you can run your main method again. See the duplicate for further details. – ManoDestra Aug 02 '16 at 13:13
  • @ManoDestra I understand, but in my case, is a web application, this hosted on Amazon, so I can not stop it and start again ... Already took a look at duplicate, but did not work – Mateus Barcelini Aug 02 '16 at 14:01
  • In which case simply use the existing application. Don't try to launch it again. It already exists. It's as simple as that :) This may be of assistance. Simply create another Stage and call start on it instead of trying to launch again. launch() will block anyway, so that's the wrong way to do it entirely. – ManoDestra Aug 02 '16 at 14:07
  • @ManoDestra Thanks for the help ... I started using the JFreeChart and it worked – Mateus Barcelini Aug 03 '16 at 13:31

0 Answers0