[英]javafx swing crashes when trying to display chart twice
我正在使用将javafx图表添加到JFrame
的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
方法重写忽略
我刚才也遇到了这个问题。 通过每次创建一个新的JFXPanel并因此在每个实例上仅设置一次场景,我设法摆脱了它。 它可以解决问题,但可能不是解决此问题的最优雅的方法。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.