[英]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.