0

So, I have this frame wherein when the user click the 'Notify' button, it will send an email to the user and I want to show a loading screen while the message was sending.

But the problem is, my system kind of lagged when sending a message and the loading screen frame that I made only shows up in the end after the email was sent even though my plan was to show the loading screen frame while sending an email, I put the code on the first line so that it will be the first one the the program will read but it still keeps showing on the last.

I found some questions here about this problem but they're not using java as me so I don't know how I can fix mine.

These codes are not created in one class/frame. I created 2 JFrames Form and 1 Java Class. I have 1 frame for notifying the users, 1 frame for my supposedly loading screen, and 1 java class with a code for sending an email with the code for loading screen.

Sending Email with Loading Screen Code Included...

public class Emailing {
    public static void SendMail(String recepient) throws Exception {

    loadingScreen loadingFrame = new loadingScreen();
    loadingFrame.setVisible(true);
    try {
        for (int i=0;i<=100;i++) {
            Thread.sleep(100);
            loadingFrame.percent.setText(i + "%");
            
            if (i == 20){
                loadingFrame.msg.setText("Preparing your message...");
            }if (i == 50){
                loadingFrame.msg.setText("Please wait a moment...");
            }if (i == 70){
                loadingFrame.msg.setText("Almost there...");
            }if (i == 100){
                loadingFrame.msg.setText("Message sent succesfully!");
                closeBTN.setVisible(true);
            }
            loadingFrame.loadingBar.setValue(i);
        }
    }catch (Exception e){
        e.printStackTrace();
    }
        Properties properties = new Properties();
        
        properties.put("mail.smtp.auth", "true");
        properties.put("mail.smtp.starttls.enable", "true");
        properties.put("mail.smtp.host", "smtp.gmail.com");
        properties.put("mail.smtp.port", "587");
        
        String myEmail = "xxxxxxxxxx@gmail.com"; //hide my email information sorry
        String myPassword = "xxxxxxxxxx";
        
        Session session = Session.getInstance(properties, new Authenticator(){
            @Override
            protected PasswordAuthentication getPasswordAuthentication() {
                return new PasswordAuthentication(myEmail, myPassword);
                    
            }
        });
        Message message = prepareMessage (session, myEmail, recepient);
        Transport.send(message);

Then my Main frame where the user will click the notify button and will then supposedly show the loading screen frame while sending a message...

private void sendNotice () throws Exception{
        Emailing.SendMail(userEmailAccount.getText());
}

//when the button was clicked
private void notifyUserButtonActionPerformed((java.awt.event.MouseEvent evt) {
        sendNotice();
}  

Is there a way where I can show a loading screen while sending a message?

  • why wouldn't there be? – Stultuske May 18 '21 at 07:26
  • 1
    You are definitely blocking the Event Dispatch Thread (EDT), which is responsible for GUI updates. Since this thread is busy with sleeping (`Thread.sleep()`) you will not see GUI updates. See [Concurency in Swing](https://docs.oracle.com/javase/tutorial/uiswing/concurrency/index.html). The tool to use in these scenarios is e.g. a [`SwingWorker`](https://docs.oracle.com/javase/8/docs/api/javax/swing/SwingWorker.html), which can do some background task and still update the GUI. – maloomeister May 18 '21 at 07:31
  • @Stultuske im not exactly sure, this is the first time I used javamail API. whenever I tried to send an email by clicking the button, there was always this pause like its loading or lagging then after that, the email was sent successfully – foo.098 May 18 '21 at 07:32
  • Or if you want to have some actions performed in a timed fashion, use a [`javax.swing.Timer`](https://docs.oracle.com/javase/8/docs/api/javax/swing/Timer.html) to perform actions with a predefined delay. [Example](https://stackoverflow.com/questions/13691339/adding-a-timer-and-displaying-label-text/13691413#13691413). You can find a working example with the `SwingWorker` [here](https://docs.oracle.com/javase/tutorial/uiswing/examples/components/ProgressBarDemoProject/src/components/ProgressBarDemo.java). – maloomeister May 18 '21 at 07:34
  • 1
    Btw, your code shows quite some lack of experience with Java, e.g. the use of a static method that also accesses buttons (which would have to be static as well), the naming (methods should start with a lower case character, so `sendMail` instead of `SendMail` while classes should start with an upper case char, so `LoadingScreen` instead of `loadingScreen`), etc. - If you really are as inexperienced as this would indicate I'd suggest revisiting those basics before diving into GUI development to save yourself a few headaches. – Thomas May 18 '21 at 07:41

0 Answers0