0

I Have this code which is a part of a larger program that sends emails which contain information about a paricular spreadsheet. The EmailOverdue.run() method is the "slow method" that I'm referring to, since it takes a few seconds to run. After that method runs, the JLabel called status is supposed to say "Sent", which works. What I'm having trouble with is the line where I say status.setText("Sending Message...");. Instead of saying that, the jlabel just doesn't change until it says sent at the end. How can I make the JLabel update right after I change the text to "Sending Message..." so that it displays that while the "slow" email method is running? My code is below, thanks for the help.

JButton submit = new JButton("Send Email");
submit.addActionListener(new ActionListener(){
    public void actionPerformed(ActionEvent ae){
            
        String daysOverFilterString = daysOver.getText();
        daysOverFilterString = daysOverFilterString.trim();
                  
        sendEmail1 = false;
        sendEmail2 = false;
        sendEmail3 = false;
        
        //email1, email2, and email3 are checkboxes created earlier
        if(email1.isSelected()) 
            sendEmail1= true;
        if(email2.isSelected())
            sendEmail2= true;
        if(email3.isSelected())
            sendEmail3= true;
                  
        try {
            daysOverFilter = Integer.valueOf(daysOverFilterString);
        } catch (NumberFormatException nfe) {
            status.setText("Please Enter a Numerical Value");
            status.setForeground(Color.RED);
            intValue = false;
        }
                  
        if(intValue && (!sendEmail1 && !sendEmail2 && !sendEmail3)) {
            status.setText("Please Select At Least One Email Address");
            status.setForeground(Color.RED);
            checkBoxes = false;
        }
                  
        if(intValue && checkBoxes) {
            status.setText("Sending Message"); //this never actually shows up, which is what I'm tryna fix
            status.setForeground(Color.ORANGE);

            try {
                //EmailOverdue is a seperate class by the way, that sorts through some data and sends an email
                EmailOverdue.run(daysOverFilter, sendEmail1, sendEmail2, sendEmail3); //this is the "slow method"
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
});
Saashin
  • 1
  • 3
  • 1
    *"The EmailOverdue.run() method is the "slow method" that I'm referring to, since it takes a few seconds to run"* -- you're running this method on the Swing event thread, blocking this thread and preventing it from doing its important actions, including updating the GUI. The solution is to run this within a background thread so as not to block and freeze the event thread, and then do the last JLabel `.setText("sent")` in a call-back of some sort. Best to use a SwingWorker to help facilitate these tasks. Read [Concurrency in Swing](https://docs.oracle.com/javase/tutorial/uiswing/concurrency/) – Hovercraft Full Of Eels Aug 07 '20 at 19:22
  • Please feel free to comment back if any of this confuses you – Hovercraft Full Of Eels Aug 07 '20 at 19:28
  • 1
    @HovercraftFullOfEels I used SwingWorker and it is working properly now. Thank you so much for your help! :) – Saashin Aug 08 '20 at 00:16
  • Yay. Glad that you've got things working OK – Hovercraft Full Of Eels Aug 08 '20 at 00:27

0 Answers0