[英]JFileChooser Thread & JLabel
我正在创建一个解析器 ,这就是为什么我使用JFileChooser的原因。 当我使用JFileChooser选择文件时,我想要一个JLabel:“正在解析”或类似的内容。 当完成时: “解析完成” 。
(我的首要目标是使用进度条,但现在对我来说有点复杂)
ReadFile类将获取文件数组并为每个文件创建Callable。 如果5个文件:将调用5个线程。 我使用Callable是因为我需要获取每个线程的数据字符串并将其写入相同的csv文件中。
好吧,当我在JFileChooser上单击“取消”时,JLabel在正确的时间正确显示,但是当我为解析功能选择文件/文件时, JLabel等待我的Callables的整个执行,然后出现“处理中”(但是当已经结束^^)。
我无法在线程开始时显示处理。
注意:此刻我调用了CardLayout,但尚未使用。
这是我的代码:
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);
}
}
好吧,@ tobias_k是正确的! 谢谢你,兄弟。
当我启动ReadFile时,它将冻结我的摆动线程,直到ReadFile完成。
我将ReadFile更改为线程(并调用可调用对象),现在它可以正常工作了。 谢谢 !
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.