我正在使用添加到JFrame
的javafx图表制作一个java swing应用程序。单击一个按钮,在新窗口中显示图表。第一次完成所有工作正常,但在关闭图表窗口并再次单击按钮时,它会在显示下一个图表窗口之前崩溃,在
fxPanel.setScene(scene);
我认为第一次关闭图表窗口时需要重置fxPanel
或scene
,但到目前为止我还没有尝试过。下面是我的图表绘图课程。任何帮助非常感谢。
public class PlotChart extends JFrame implements WindowListener {
private JFrame chartWindow = new JFrame("Commitment Of Traders");
public PlotChart(JFXPanel fxPanel) {
plotChart(fxPanel);
}
public void plotChart(JFXPanel fxPanel) {
final NumberAxis xAxis = new NumberAxis();
final NumberAxis yAxis = new NumberAxis();
final LineChart<Number,Number> lineChart = new LineChart<Number,Number>(xAxis,yAxis);
final Scene scene = new Scene(lineChart,800,600);
xAxis.setLabel("Week");
lineChart.setTitle("Commitment of Traders");
//Define a series
XYChart.Series commercials = new XYChart.Series<>();
XYChart.Series nonCommercials = new XYChart.Series<>();
XYChart.Series CLong = new XYChart.Series<>();
XYChart.Series CShort = new XYChart.Series<>();
commercials.setName("Dealers");
nonCommercials.setName("Hedge Funds");
//Populate series
for (int i=0;i<ReadCSV.getCommercials().size();i++) {
commercials.getData().add(new XYChart.Data(i,ReadCSV.getCommercials().get(i)));
nonCommercials.getData().add(new XYChart.Data(i,ReadCSV.getNonCommercials().get(i)));
}
lineChart.getData().add(commercials);
lineChart.getData().add(nonCommercials);
fxPanel.setScene(scene); // exception in this line
chartWindow.add(fxPanel);
chartWindow.setSize(800, 600);
chartWindow.setVisible(true);
chartWindow.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
chartWindow.addWindowListener(new java.awt.event.WindowAdapter() {
public void windowClosing(java.awt.event.WindowEvent windowEvent) {
ReadCSV.clearVars();
System.out.println("window closed");
}
});
}
WindowListener
方法覆盖省略
答案 0 :(得分:0)
我刚才也遇到过这个问题。我设法通过每次创建一个新的JFXPanel来摆脱它,因此在每个实例上只设置一次场景。 它解决了这个问题,但可能并不是解决这个问题最优雅的方法。