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?