0

I'm working with code that is essentially a file mover program. What I'm attempting to do is after the user clicks the submit button which calls the file mover, the text of the button would change to 'Working'. I have a basic understanding of why it didn't work when I just set it, but I tried to use SwingUtilities to call it in the background. However, it still waits until after the method call ft.FindSpot has completed before displaying any changes.

            public void actionPerformed(ActionEvent arg0) {
            if(!textField.getText().equals(""))
            {
                SwingUtilities.invokeLater(new Runnable() {
                    public void run() {
                        btnSubmit.setText("Working...");
                    }
                });
                //btnSubmit.setText("Working...");
                ft.FindSpot(textField.getText(), comboBox.getSelectedItem().toString(), progressBar);

                SwingUtilities.invokeLater(new Runnable() {
                    public void run() {
                        btnSubmit.setText("Submit");
                    }
                });
            }
            else
            {
                ft.warningMessage("The ISCII textbox cannot be blank.");
            }
        }
    });
Frakcool
  • 10,915
  • 9
  • 50
  • 89
Marshal Alessi
  • 105
  • 2
  • 2
  • 11
  • 3
    Use a [Swing Worker](https://docs.oracle.com/javase/7/docs/api/javax/swing/SwingWorker.html) to do the long process on a background thread. The [tutorial](https://docs.oracle.com/javase/tutorial/uiswing/concurrency/worker.html) shows an example – Frakcool Oct 17 '17 at 19:05
  • 1
    the `actionPerformed()` method is called on the Swing Events Thread. So, the first `btnSubmit.setText("working…")` would not need to be called with `invokeLater()`. But `ft.FindSpot` also runs on the Swing Events Thread, essentially blocking it until is has finished, and thus also no visual update of the button can take place. – cello Oct 17 '17 at 19:09
  • @MarshalAlessi here's an [example](https://stackoverflow.com/questions/42256369/update-jlabel-text-after-opening-jdialog/42256976#42256976) it's not a [mcve] in my answer but you may take the question's code and use my recommendations in the answer I provided. However at this moment I'm unable to provide an example but could try in a couple more hours – Frakcool Oct 17 '17 at 20:47
  • Thank you @Frakcool. I will review the information for future purposes. However, I was able to find a solution for this problem as I posted below. Thanks again! – Marshal Alessi Oct 17 '17 at 20:50
  • Give it a try and let me know if you managed to create one from there, else I'll post my answer with a new MCVE (and I suggest you to create a [mcve] for your future questions and not code snippets) – Frakcool Oct 17 '17 at 20:51

1 Answers1

0

I managed to fix the issue by putting the ft.FindSpot in the invokeLater Swing Utilities function so that it was called from a different thread.

        btnSubmit.setFont(new Font("Verdana", Font.PLAIN, 12));
    btnSubmit.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent arg0) {
            if(!textField.getText().equals(""))
            {
             btnSubmit.setText("Working...");
                SwingUtilities.invokeLater(new Runnable() {
                    public void run() {
                        ft.FindSpot(textField.getText(), comboBox.getSelectedItem().toString(), progressBar);
                    }
                });

                SwingUtilities.invokeLater(new Runnable() {
                    public void run() {
                        btnSubmit.setText("Submit");
                    }
                });
            }
            else
            {
                ft.warningMessage("The ISCII textbox cannot be blank.");
            }
        }
    });
Marshal Alessi
  • 105
  • 2
  • 2
  • 11