Answer the question
In order to leave comments, you need to log in
Java UI thread programming?
I'm trying to write a program to send messages to an e-mail address using swing NetBeans. In principle, everything works, the problem is as follows:
There is a list of recipients in the table, all of them need to send the same message from jTextArea and those who are sent to put the status "Sent" in the table and fill jProgressBar in real time. Sending occurs at the click of a button. If you write these actions sequentially, then the interface hangs until the end of the send cycle. I tried to move this cycle to a separate thread, the result is the same. How to be?
Here is the code:
List<String> sendMail = new ArrayList<>();
for (int i=0;i<jTable1.getRowCount();i++){
if ((Boolean)jTable1.getValueAt(i, 0)) sendMail.add((String)jTable1.getValueAt(i, 1));
}
String subject = jTextField1.getText();
String msg = jTextArea2.getText();
jProgressBar1.setMinimum(0);
jProgressBar1.setMaximum(sendMail.size());
jProgressBar1.setValue(0);
Properties props = new Properties();
props.setProperty("mail.smtp.host", "smtp.yandex.ru");
props.setProperty("mail.smtp.port", "465");
props.setProperty("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
props.setProperty("mail.smtp.socketFactory.port", "465");
props.setProperty("mail.smtp.auth", "true");
props.setProperty("user", "user");
props.setProperty("pwd", "*****");
int i =0;
SendUtils sm = new SendUtils(props);
for (String mail:sendMail){
try{
sm.send(mail, subject, msg);
jTable1.setValueAt(new ImageIcon("ok.png"), i, 2);
}catch(MessagingException e){
jTable1.setValueAt(new ImageIcon("no.png"), i, 2);
}
jProgressBar1.setValue(++i);
}
new Thread(new Runnable() {
@Override
public void run() {
List<String> sendMail = new ArrayList<>();
for (int i=0;i<jTable1.getRowCount();i++){
if ((Boolean)jTable1.getValueAt(i, 0)) sendMail.add((String)jTable1.getValueAt(i, 1));
}
String subject = jTextField1.getText();
String msg = jTextArea2.getText();
jProgressBar1.setMinimum(0);
jProgressBar1.setMaximum(sendMail.size());
jProgressBar1.setValue(0);
Properties props = new Properties();
props.setProperty("mail.smtp.host", "smtp.yandex.ru");
props.setProperty("mail.smtp.port", "465");
props.setProperty("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
props.setProperty("mail.smtp.socketFactory.port", "465");
props.setProperty("mail.smtp.auth", "true");
props.setProperty("user", "[email protected]");
props.setProperty("pwd", "iNg9quup");
int i =0;
SendUtils sm = new SendUtils(props);
for (String mail:sendMail){
try{
sm.send(mail, subject, msg);
jTable1.setValueAt(new ImageIcon("ok.png"), i, 2);
}catch(MessagingException e){
jTable1.setValueAt(new ImageIcon("no.png"), i, 2);
}
jProgressBar1.setValue(++i);
}
}
}).run();
Answer the question
In order to leave comments, you need to log in
I decided everything, instead of run I called start, I beguiled the launch of the thread)
To do this, the SwingUtilities class is made in swing, and specifically, the invokeLater method. The documentation recommends using it.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question