如果我在doInBackground()中有一个属于其他类的方法,是否可以根据外部类的其他方法中发生的更改来设置setProgess()?
我创建了一个创建JFileChooser的按钮,以便用户可以打开.txt文件,这里是按钮动作监听器内的代码:
JFileChooser fc = new JFileChooser();
//filter-show only .txt files
FileNameExtensionFilter txtfilter = new FileNameExtensionFilter("txt files (*.txt)", "txt");
//apply the filter to file chooser
fc.setFileFilter(txtfilter);
fc.setDialogTitle("Otvori txt file");
//disable the ability to show files of all extensions
fc.setAcceptAllFileFilterUsed(false);
//create file chooser via jFrame
fc.showOpenDialog(jFrame);
//get selected file
File selFile = fc.getSelectedFile();
Path path = Paths.get(selFile.toString());
asdf = selFile.toString();
//display chosen file on jLabel5
jLabel5.setText(path.getFileName().toString());
Run Code Online (Sandbox Code Playgroud)
它可以正常工作,如果您在文件选择器中选择.txt文件,但如果您只选择一个文件然后按取消并退出它也可以.我认为这是因为getSelectedFile(),但我想知道是否有办法确保用户选择一个文件并在文件选择器中按下打开作为获取文件的条件?