我正在使用線程來解決GUI凍結的問題。但與線程我面臨一個問題,我無法通過運行方法的參數或甚至在構造函數的幫助下傳遞格式的報告,我無法做到這一點.....JavaFX線程問題
public class BirtReportExportCon implements Runnable {
@FXML
Button exportButton;
@FXML
CheckBox pdfCheckBox;
@FXML
CheckBox xlsCheckBox;
@FXML
CheckBox docCheckBox;
@FXML
CheckBox mailCheckBox;
public String fileFormat;
允許檢查桂單個複選框
public void eventCheckBoxPdf() {
if (pdfCheckBox.isSelected() == true) {
xlsCheckBox.setSelected(false);
docCheckBox.setSelected(false);
}
}
public void eventCheckBoxXls() {
if (xlsCheckBox.isSelected() == true) {
pdfCheckBox.setSelected(false);
docCheckBox.setSelected(false);
}
}
public void eventCheckBoxDoc() {
if (docCheckBox.isSelected() == true) {
pdfCheckBox.setSelected(false);
xlsCheckBox.setSelected(false);
}
}
提供救世FILEFORMAT
public void onButtonClick() throws EngineException {
if (docCheckBox.isSelected() == true) {
fileFormat = "docx"; // I WANT THIS FILE FORMAT IN MY RUN METHOD
Runnable r = new BirtReportExportCon();
new Thread(r).start();
}
else if (pdfCheckBox.isSelected() == true) {
fileFormat = "pdf";
Runnable r = new BirtReportExportCon();
new Thread(r).start();
}
else if (xlsCheckBox.isSelected() == true) {
fileFormat = "xls";
Runnable r = new BirtReportExportCon();
new Thread(r).start();
}
}
運行方法
public void run()
{
try
{
exportFile(fileFormat); // HERE I WANT THAT SO I CAN ABLE TO CREATE REPORT OF REQUIRED FORMAT
}
catch (EngineException e) {
e.printStackTrace();
}
}
保存報表和打開報表
public void exportFile(String fileFormat) throws EngineException {
String output = "output path";
String reportDesignFilePath = "report path";
try {
EngineConfig configure = new EngineConfig();
Platform.startup(configure);
IReportEngineFactory reportEngineFactory = (IReportEngineFactory) Platform
.createFactoryObject(IReportEngineFactory.EXTENSION_REPORT_ENGINE_FACTORY);
IReportEngine engine = reportEngineFactory.createReportEngine(configure);
engine.changeLogLevel(Level.WARNING);
IReportRunnable runnable = engine.openReportDesign(reportDesignFilePath);
IRunAndRenderTask task = engine.createRunAndRenderTask(runnable);
IRenderOption option = new PDFRenderOption();
option.setOutputFormat(fileFormat);
option.setOutputFileName(output + fileFormat);
task.setRenderOption(option);
task.run();
task.close();
} catch (Exception e) {
e.printStackTrace();
}
// Open Created File
File fileOpen = new File(output + fileFormat);
if (fileOpen.exists()) {
if (Desktop.isDesktopSupported()) {
try {
Desktop desktop = Desktop.getDesktop();
desktop.open(fileOpen);
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
謝謝@卡爾納 – Demo
有幫助嗎?如果 – karna
是的,並且實際的問題是我無法在我的線程運行方法中傳遞參數。但是現在我已經解決了這個問題,我期待優化代碼 – Demo