I'm creating a parser that's why I use JFileChooser. When I select a file with JFileChooser, I would like to have a JLabel that says : "parsing in progress" or smth like that. 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. If 5 files : 5 threads will be called. I used Callable because I need to get back Strings of data for each Threads and write it in a same csv file.
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 ^^).
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.
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 ! Thank you man.
When I launched ReadFile, that was freezing my swing thread until ReadFile was done.
I changed ReadFile to a thread (and calling callables), and now it works perfectly. Thanks !
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.