我正在创建一个Eclipse-Plugin,我需要打开文件系统浏览器,选择一个文件然后对所选文件执行某些计算。
我尝试过使用JFileChooser
(我有一个打开文件浏览器的类)。从我的处理程序类调用这个特定的类,一旦选择了文件(从文件选择器),我需要返回String(文件位置)并对其执行计算。但是,我遇到了这方面的困难,因为我在关闭JFrame
后无法执行此计算。
public class FileChooserDialog {
private JFrame mainFrame;
private JLabel headerLabel;
private JLabel statusLabel;
private JPanel controlPanel;
private String selectedFile = null;
public FileChooserDialog(){
prepareGUI();
clickExtractFile();
}
public String getSelectedFile(){
return selectedFile;
}
private void prepareGUI(){
mainFrame = new JFrame("choose a file");
mainFrame.setSize(400,400);
mainFrame.setLayout(new GridLayout(3, 1));
headerLabel = new JLabel("", JLabel.CENTER);
headerLabel.setText("Choose a valid zip file to extract");
statusLabel = new JLabel("",JLabel.CENTER);
statusLabel.setSize(350,100);
statusLabel.setText("No file selected");
controlPanel = new JPanel();
controlPanel.setLayout(new FlowLayout());
mainFrame.add(headerLabel);
mainFrame.add(controlPanel);
mainFrame.add(statusLabel);
mainFrame.setVisible(true);
}
private void clickExtractFile(){
final JFileChooser fileDialog = new JFileChooser();
JButton showFileDialogButton = new JButton("Open File");
showFileDialogButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
int returnVal = fileDialog.showOpenDialog(mainFrame);
if (returnVal == JFileChooser.APPROVE_OPTION) {
File file = fileDialog.getSelectedFile();
statusLabel.setText(file.getAbsolutePath());
}
else{
statusLabel.setText("No file selected");
}
}
});
JButton extractButton = new JButton("Choose This");
extractButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
selectedFile = statusLabel.getText();
if (selectedFile.equals("No file selected")){
selectedFile = "No file";
System.out.println("nahh nothing selected");
}else{
System.out.println("yes a file was selected. This was: " + selectedFile);
}
mainFrame.dispatchEvent(new WindowEvent(mainFrame, WindowEvent.WINDOW_CLOSING));
}
});
controlPanel.add(showFileDialogButton);
controlPanel.add(extractButton);
mainFrame.setVisible(true);
}
处理程序类:
public class Handler extends AbstractHandler{
FileChooserDialog chooser = new FileChooserDialog();
chooser.clickExtractFile();
//I only want this to be called once the Jframe is closed (i.e. after user clicks "Choose This"
fileChosen = chooseZip.getSelectedFile();
}
然而,当fileChosen = chooseZip.getSelectedFile();
被执行时,我收到一个nullpointer异常,因为它甚至在关闭JFrame之前就被执行了。
例外:
org.eclipse.e4.core.di.InjectionException: java.lang.NullPointerException
at org.eclipse.e4.core.internal.di.MethodRequestor.execute(MethodRequestor.java:62)
at org.eclipse.e4.core.internal.di.InjectorImpl.invokeUsingClass(InjectorImpl.java:247)
at org.eclipse.e4.core.internal.di.InjectorImpl.invoke(InjectorImpl.java:229)
at org.eclipse.e4.core.contexts.ContextInjectionFactory.invoke(ContextInjectionFactory.java:132)
at org.eclipse.e4.core.commands.internal.HandlerServiceHandler.execute(HandlerServiceHandler.java:149)
at org.eclipse.core.commands.Command.executeWithChecks(Command.java:499)
at org.eclipse.core.commands.ParameterizedCommand.executeWithChecks(ParameterizedCommand.java:508)
at org.eclipse.e4.core.commands.internal.HandlerServiceImpl.executeHandler(HandlerServiceImpl.java:210)
at org.eclipse.e4.ui.workbench.renderers.swt.HandledContributionItem.executeItem(HandledContributionItem.java:825)
at org.eclipse.e4.ui.workbench.renderers.swt.HandledContributionItem.handleWidgetSelection(HandledContributionItem.java:701)
at org.eclipse.e4.ui.workbench.renderers.swt.HandledContributionItem.access$6(HandledContributionItem.java:685)
at org.eclipse.e4.ui.workbench.renderers.swt.HandledContributionItem$4.handleEvent(HandledContributionItem.java:613)
at org.eclipse.swt.widgets.EventTable.sendEvent(EventTable.java:84)
我该如何解决这个问题?