Delay in displaying contents in JDialog

Posted by Yohan on Stack Overflow See other posts from Stack Overflow or by Yohan
Published on 2012-09-28T15:32:13Z Indexed on 2012/09/28 15:37 UTC
Read the original article Hit count: 297

Filed under:
|
|
|
|

Please have a look at the following code

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.List;
import java.util.ArrayList;

public class SendEmailForm extends JDialog {

    private JLabel to, cc, bcc, subject, account;
    private JTextField toTxt, ccTxt, bccTxt, subjectTxt;
    private JTextArea messageTxt;
    private JButton send;
    private JComboBox accountBox;
    private JScrollPane scroll;
    private GridBagLayout gbl;
    private GridBagConstraints gbc;

    public SendEmailForm() {
        //Declaring instance variables
        to = new JLabel("To: ");
        cc = new JLabel("CC: ");
        bcc = new JLabel("BCC: ");
        subject = new JLabel("Subject: ");
        account = new JLabel("Select an Account: ");

        toTxt = new JTextField(20);
        ccTxt = new JTextField(20);
        bccTxt = new JTextField(20);
        subjectTxt = new JTextField(20);

        messageTxt = new JTextArea(20, 50);
        messageTxt.setLineWrap(true);
        scroll = new JScrollPane(messageTxt);
        scroll.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
        scroll.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);

        accountBox = new JComboBox();
        accountBox.addItem("Yahoo");
        accountBox.addItem("GMail");
        accountBox.addItem("MSN");
        //accountBox.addItem("Yahoo");
        //accountBox.addItem("Yahoo");

        JPanel buttonPanel = new JPanel();
        buttonPanel.setLayout(new FlowLayout());

        send = new JButton("Send");
        send.addActionListener(new SendButtonAction());
        buttonPanel.add(send);

        //Creating thr GUI
        //GUI CREATION IS REMOVED IN THIS POST

        this.setTitle("Send Emails");
        this.setVisible(true);
        this.pack();
        this.setLocationRelativeTo(null);
        this.validate();
    }

    private class SendButtonAction implements ActionListener {

        public void actionPerformed(ActionEvent ae) {
            ProgressMonitor pm = new ProgressMonitor();
            //Retreiving the user name and password
            List userData = new ArrayList();

            EmailDBConnector emailCon = new EmailDBHandler();
            userData = emailCon.getUserNameAndPassword(
                    accountBox.getSelectedItem().toString().trim());

            String userName = userData.get(0).toString();
            String password = userData.get(1).toString();
            System.out.println(userName);
            System.out.println(password);

            pm.setVisible(true);

            SendEmail sendEmail = new SendEmail(toTxt.getText(), userName.trim(),
                    bccTxt.getText(), ccTxt.getText(), accountBox.getSelectedItem().toString().trim(), messageTxt.getText().trim(),
                    password.trim(), subjectTxt.getText());

            String result = sendEmail.send();
            //pm.dispose();
            JOptionPane.showMessageDialog(null, result);
        }
    }

    private class ProgressMonitor extends JDialog {

        public ProgressMonitor() {
            this.setLayout(new BorderLayout());
            JLabel text = new JLabel("Sending..Please wait...");
            this.add(text, "Center");
            this.pack();
            this.validate();
            this.setLocationRelativeTo(null);
        }
    }
}

First, this is an email program. In here, when the JDialog is called, it just opens as a 100% blank window. I have added a JLabel, but it is not there when it is displaying. Anyway, it takes sometime to send the email, after the email is sent, I can see the JLabel in the JDialog. If I take my issue into one sentence, I am calling the JDialog before the email is sent, but it appears blank, after the email is sent, it's content are there! Why is this? Please help!

© Stack Overflow or respective owner

Related posts about java

Related posts about swing