如何在JfreeChart SaveAsPNG方法中实现SaveAs对话框

时间:2015-08-02 18:17:09

标签: java eclipse swt jfreechart rcp

拜托,我需要你的帮助。你能告诉我如何在下一个代码中实现SWT SaveAs对话框吗?我需要用户可以选择他想要保存图表的位置。谢谢!

 try {
                File file = new File("mychart.png");
                float calidad = 1;

                ChartUtilities.saveChartAsJPEG(file, calidad, chart, 800, 600);
                MessageDialog.openInformation(shell, "Save Chart", "The file has been saved");

            } catch (IOException e1) {
                e1.printStackTrace();
                MessageDialog.openInformation(shell, "Save Chart", "Error saving file. Please try again...");
            }

1 个答案:

答案 0 :(得分:2)

使用SWT FileDialog - 类似于:

Shell shell = ... current shell

FileDialog fileDialog = new FileDialog(shell, SWT.SAVE);

fileDialog.setFilterExtensions(new String [] {"*.png", "*.*"});

fileDialog.setFilterPath(.... any default path you want ....);

String filePath = fileDialog.open();

// TODO check for null 'filePath' - user canceled the save

File file = new File(filePath);

ChartUtilities.saveChartAsPNG(file, calidad, chart, 800, 600);