1

I've been having some issues with making a GUI for a program. Back-end program works fine but sometimes it takes a while to complete.

In the GUI I want to show the user that work is being done and thought an indeterminate JProgressBar (progress) would suffice - it's indeterminate as I only want an indicator that something is happening and not actually follow the real progress. The bar has already been added to a frame so it's always there, and it's just unhidden when the button is clicked and the process starts. It should then be re-hidden once the process has finished. I've also put in some text that also should show that the process is running, which is then changed to a completed bit after it's run.

However, it's not working as intended. What seems to be happening is that the code appears to be executing in the wrong order. The c.run() instruction seems to run before the setVisible(true) one does as the progress bar only appears once the run command has finished, and I can't see why. The run() command is quite a large operation so takes a while (it's only a single threaded application), but I don't know why it would be doing this as the code definitely runs in the right order when you use print debugging to see which instructions run in which order.

but.addActionListener(new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent e) {
            runCSVReader(e);    
        }

    });


private void runCSVReader(ActionEvent evt) {
    progress.setVisible(true);
    label8.setForeground(Color.YELLOW);
    label8.setText("Processing...");
    correctInput();
    String accTypes = tf5.getText() + "\\" + tf6.getText() + ".csv";
    String inputFile = tf.getText() + "\\" + tf4.getText() + ".csv";
    String outputFile = tf7.getText() + "\\" + tf2.getText();
    System.out.println("acctypes " + accTypes+"\ninput " + inputFile + "\noutput " + outputFile);


    try {
        csvreader c = new csvreader();
        c.setAccTypesFile(accTypes);
        c.setInputFile(inputFile);
        c.setOutputFile(outputFile);
        c.run();
    //  progress.setVisible(false);
        label8.setForeground(Color.GREEN);
        label8.setText("Complete");
        //pb.end();
    } 

It's just weird as it almost seems like the code runs in the order

c.run();
progress.setVisible(true);
label8.setForeground(Color.YELLOW);
label8.setText("Processing...");
//  progress.setVisible(false);
label8.setForeground(Color.GREEN);
label8.setText("Complete");

as the text changes straight to the green completion message (with only the yellow message showing for a very short time) and (when uncommented) the setvisible go straight to false. Any idea on why this might be happening? As far as I can tell there aren't any conflicting calls within the called methods that should affect this. Is it the case that because it's a single-threaded program the progress bar won't show until the main thread is free from the run() command, and if so, why is it that the text also appears to not run when first called?

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
  • Don't block the EDT (Event Dispatch Thread). The GUI will 'freeze' when that happens. See [Concurrency in Swing](https://docs.oracle.com/javase/tutorial/uiswing/concurrency/) for details and the fix. – Andrew Thompson Aug 30 '18 at 14:04
  • I have answered a similar post. Take a look: https://stackoverflow.com/a/52080227/6579265 – George Z. Aug 30 '18 at 14:22
  • 1
    thanks guys, used a different thread to run the method and it works now – Tom Crawford Aug 30 '18 at 15:39

0 Answers0