Is it possible to draw graphs using JavaFX in a normal Java application?

≡放荡痞女 提交于 2020-01-06 04:04:10

问题


I want to draw some graphs of statistics gathered during a simulation I coded in Java. I decided to try JavaFX, as it has some great graphing abilities. However, as I've never used it before, I'm not sure if it's even possible to add JavaFX capabilities to a project that wasn't set up to do this initially.

I've added the javafx library to my project, and copy pasted the JavaFX tutorial on line graphs at http://docs.oracle.com/javafx/2/charts/line-chart.htm (without the main function) to test if the graphs display properly.

They don't, however. I call the graph in my runsimulation function (which is called from a SwingWorker thread) with

LineChartSample.launch(new String());

and after running the program and not seeing any graph, I added a println after the call and noticed that the program doesn't ever reach it; i.e. it terminates on the call to LineChartSample.

Am I doing something wrong? Is what I'm trying even possible?

EDIT: A quick summary of what that part of the code looks like:

A JButton in class InputGUI calls

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
     new AnswerWorker().execute();
}


public class AnswerWorker extends SwingWorker<Void, Integer> {

protected Void doInBackground() throws Exception
{
    AMEC.runsimulation();
    return null;
}
protected void done()
{
try {
    JOptionPane.showMessageDialog(InputGUI.this, AMEC.unsuccesfulpercentage + "% of iterations had trucks that had to sleep over");
    AMEC.unsuccesfulpercentage = 0;
    }
    catch (Exception e)
    {
        e.printStackTrace();
    }

}

And AMEC.runsimulation calls

public static void runsimulation() throws IOException, InterruptedException {
...
    LineChartSample.launch(new String());
}

I get the JDialogBox that AnswerWorker throws when it's done, but no graph, and whenever I test for a println after my LineChartSample.launch call, it never gets reached.


回答1:


try

public static void runsimulation() throws IOException, InterruptedException {
    ...
    LineChartSample.launch(LineChartSample.class, "");
}    

for more information:

http://docs.oracle.com/javafx/2/api/javafx/application/Application.html#launch%28java.lang.String...%29

be aware that you shouldn't actually do it like this, because you can't call this code more than once in your applications lifetime. Instead, you should extract the code from the LineChartSample that builds the scene graph and use a JFXPanel to embed the scene graph into your swing application.



来源:https://stackoverflow.com/questions/21132278/is-it-possible-to-draw-graphs-using-javafx-in-a-normal-java-application

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!