[英]JFileChooser Thread & JLabel
I'm creating a parser that's why I use JFileChooser. 我正在创建一个解析器 ,这就是为什么我使用JFileChooser的原因。 When I select a file with JFileChooser, I would like to have a JLabel that says : "parsing in progress" or smth like that. 当我使用JFileChooser选择文件时,我想要一个JLabel:“正在解析”或类似的内容。 And when it's done : "parsing done" . 当完成时: “解析完成” 。
(My first aim was to use progress bars, but it's a bit complicated for me now) (我的首要目标是使用进度条,但现在对我来说有点复杂)
The ReadFile class will take array of files and create Callable for each file. ReadFile类将获取文件数组并为每个文件创建Callable。 If 5 files : 5 threads will be called. 如果5个文件:将调用5个线程。 I used Callable because I need to get back Strings of data for each Threads and write it in a same csv file. 我使用Callable是因为我需要获取每个线程的数据字符串并将其写入相同的csv文件中。
Well, when I click on Cancel on JFileChooser, the JLabel displays correctly at the right moment but when I select files / file for the parsing function, the JLabel waits the entire execution of my Callables and then "processing" appears (but when it has already ended ^^). 好吧,当我在JFileChooser上单击“取消”时,JLabel在正确的时间正确显示,但是当我为解析功能选择文件/文件时, JLabel等待我的Callables的整个执行,然后出现“处理中”(但是当已经结束^^)。
I cannot manage to display processing at the beginning of the threads. 我无法在线程开始时显示处理。
Note : I called CardLayout at this moment, but it is not used yet. 注意:此刻我调用了CardLayout,但尚未使用。
Here is my code : 这是我的代码:
public class Main {
private static final String CARD_MAIN = "Card Main";
private static final String CARD_FILE = "Card File";
public static void main(String[] args) throws IOException {
createGUI();
}
public static void createGUI(){
// the JFrame
final JFrame window = new JFrame();
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
window.setLocationRelativeTo(null);
window.setTitle("TMG Parser - Thales");
window.setSize(400, 100);
// the buttonPanel ( one to open JFileChooser & one to quit )
JPanel container = new JPanel();
JPanel buttonPanel = new JPanel();
final JButton fileButton = new JButton("Choose File");
fileButton.setBackground(Color.BLACK);
fileButton.setForeground(Color.WHITE);
final JButton quitButton = new JButton("Quit");
quitButton.setBackground(Color.RED);
quitButton.setForeground(Color.WHITE);
// adding buttons to panel
buttonPanel.add(fileButton);
buttonPanel.add(quitButton);
// the status label that says : processing or done
final JLabel status = new JLabel();
container.add(status);
fileButton.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent ae)
{
JFileChooser dialogue = new JFileChooser(new File("."));
dialogue.setMultiSelectionEnabled(true) ;
if (dialogue.showOpenDialog(null)==
JFileChooser.APPROVE_OPTION) {
status.setText("Processing");
File[] fichiers=dialogue.getSelectedFiles();
for( int i = 1; i<fichiers.length; ++i){
fichiers[i].getName();
fichiers[i].getAbsolutePath();
}
// calling my execution function (threads)
ReadFile readProgram = new ReadFile(fichiers);
}
else{status.setText("Action cancelled");}
}
});
quitButton.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent ae)
{
System.exit(0);
}
});
window.add(container, BorderLayout.CENTER);
window.add(buttonPanel, BorderLayout.PAGE_END);
window.setVisible(true);
}
}
Ok so @tobias_k was right ! 好吧,@ tobias_k是正确的! Thank you man. 谢谢你,兄弟。
When I launched ReadFile, that was freezing my swing thread until ReadFile was done. 当我启动ReadFile时,它将冻结我的摆动线程,直到ReadFile完成。
I changed ReadFile to a thread (and calling callables), and now it works perfectly. 我将ReadFile更改为线程(并调用可调用对象),现在它可以正常工作了。 Thanks ! 谢谢 !
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.