JavaFX layout not updating and email sending optimization problem

In summary, the conversation discusses the development of a Java application for bulk emailing. The first issue mentioned is the slow performance, taking around 15 seconds to send 5 emails. The second, more important issue is with the JavaFX not updating the scene as intended. The code provided shows the use of the handleSend() method for the button and the use of properties and sessions for sending emails. The programmer also seeks advice on solving these issues, with a possible solution being to address a concurrency problem.
  • #1
archaic
688
214
I am writing a java application that would let me bulk send emails.

The first problem I have is that of performance; approximately 15 seconds per 5 emails.

The second problem, which is the more important, is that my JavaFX is not updating the scene. My code below shows that the way I intended this to work is that it should display the email that is currently being treated, and that the "Envoyés" and "Échecs" labels get updated each time.

handleSend() is the onClick() like method for the button basically.

Java:
package test;

import java.io.UnsupportedEncodingException;
import java.util.Properties;

import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.NoSuchProviderException;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.scene.control.Label;
import javafx.scene.control.TextArea;

public class AppController {

    @FXML
    private TextArea emails;

    @FXML
    private Label sent;

    @FXML
    private Label failed;

    @FXML
    private Label sending;

    @FXML
    void handleSend(ActionEvent event) {
        String[] emailsList = emails.getText().split("\n", -1);
        //emails.setDisable(true);
        
        Properties props = System.getProperties();
        props.put("mail.smtp.starttls.enable", "true");
        props.put("mail.smtp.host", "my host");
        props.put("mail.smtp.user", "my email");
        props.put("mail.smtp.password", "my password");
        props.put("mail.smtp.port", "587");
        props.put("mail.smtp.auth", "true");

        Session session = Session.getDefaultInstance(props);

        Transport transport;
        try {
            transport = session.getTransport("smtp");
            transport.connect("smtp", "my email", "my password");
            
            int count = 0; // to see how many get sent
            long time = System.currentTimeMillis(); // for the runtime
            for (int i = 0; i < emailsList.length; ++i) {
                sending.setText("En cours: " + emailsList[i]); // the email that is being treated
                MimeMessage message = new MimeMessage(session);

                try {
                    message.setFrom(new InternetAddress("my email", "what i want others to see as my name"));
                } catch (UnsupportedEncodingException | MessagingException e) {
                    e.printStackTrace();
                }

                // Transport transport;
                try {
                    message.setSubject("subject");
                    message.setContent("big html paragraph", "text/html; charset=utf-8");
                    message.addRecipient(Message.RecipientType.TO, new InternetAddress(emailsList[i]));

                    transport.sendMessage(message, message.getAllRecipients());
                    System.out.println("Mail " + (i + 1) + " enoyé avec succès à " + emailsList[i]); // for the console
                    sent.setText("Envoyés: " + (++count)); // number of sent mails updated each iteration
                    failed.setText("Échecs: " + (i + 1 - count)); // the same for the fails
                } catch (MessagingException e) {
                    e.printStackTrace();
                }
            }
            transport.close();

            time = System.currentTimeMillis() - time;
            sending.setText(String.format("%d mail(s) envoyés en %.2fs.", count, ((double) time) / 1000));
            // total count of emails that got sent + runtime
        } catch (NoSuchProviderException e) {
            e.printStackTrace();
        } catch (MessagingException e) {
            e.printStackTrace();
        }
        
        //emails.setDisable(false);
    }

    @FXML
    void initialize() {
        sending.setText("");
        sent.setText("Envoyés: 0");
        failed.setText("Échecs: 0");
    }
}

The GUI is as such (the sending label is hidden when the program launches, of course):
lt3o7.png

Any advice on how to solve these problems is most welcome, especially the second. Thank you very much for your time!
 
Technology news on Phys.org

1. Why is my JavaFX layout not updating?

There could be several reasons for this issue. Some common causes include having incorrect bindings or listeners set up, not properly updating the UI thread, or not using the proper methods to update the layout. Another potential issue could be not calling the layout's requestLayout() method after making changes. It's important to carefully review your code and ensure that all necessary updates are being made and that the proper methods are being called.

2. How can I optimize email sending in JavaFX?

One way to optimize email sending in JavaFX is by using a separate thread for the sending process. This will prevent the UI from freezing while the email is being sent. Additionally, you can use a library such as JavaMail to handle the sending process, which can improve efficiency and provide more features. It's also important to properly handle any errors or exceptions that may occur during the sending process.

3. Can I use layout animations in JavaFX?

Yes, JavaFX provides built-in support for animations within layout transitions. You can use the Transition class and its subclasses to create animations for layout changes such as resizing, moving, or adding/removing nodes. It's important to note that animations can impact performance, so it's important to use them sparingly and carefully optimize them for your specific needs.

4. How can I improve the performance of my JavaFX layout?

One way to improve the performance of your JavaFX layout is by using a virtualized control, such as ListView or TableView, for large amounts of data. This will only load and display the visible data, reducing the memory and processing required. Another way to improve performance is by using the Node.setCache() method to cache complex nodes, reducing the need for frequent redraws. It's also important to avoid unnecessary bindings and listeners, as these can impact performance as well.

5. How can I debug layout issues in JavaFX?

To debug layout issues in JavaFX, you can use the built-in LayoutBounds and LayoutFlags properties to see the size and location of each node within the layout. Additionally, you can use the SceneBuilder tool to visually inspect and modify your layout. It's also helpful to use the GridPane.setGridLinesVisible() method to see the boundaries of each cell in a GridPane, which can help identify any incorrect layout positioning.

Similar threads

  • Programming and Computer Science
Replies
1
Views
1K
  • Computing and Technology
Replies
4
Views
3K
Replies
12
Views
6K
Back
Top