0

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!

mKorbel
  • 109,525
  • 20
  • 134
  • 319
PeakGen
  • 21,894
  • 86
  • 261
  • 463

2 Answers2

3

your code isn't an SSCCE, then

1.SendEmailForm();

  • remove this.validate();

  • move this.setVisible(true); as last code line

  • move this.pack(); before this.setVisible(true);

  • move this.setLocationRelativeTo(null); before this.setVisible(true);

2.public ProgressMonitor() { coud be

code

    private class ProgressMonitor extends JDialog {

        public ProgressMonitor() {
            setLayout(new BorderLayout());
            JLabel text = new JLabel("Sending..Please wait...");
            add(text, "Center");
            pack();
            setLocationRelativeTo(null);
            SwingUtilities.invokeLater(new Runnable() {

                @Override
                public void run() {
                    setVisible(true);
                }
            });
        }
    }

.

3.rest isn't clear from your question

  • are JComponents added to the visible container, then last code lines should be (re)validate() and repaint()

  • reuse JDialog for next action, set HIDE_on_CLOSE as default close operation for JDialog

  • don't extend JDialog, create this contianer as local variable, then you can call only (my)JDialog.setVisible(true) on next action (wrapped in invokeLater())

mKorbel
  • 109,525
  • 20
  • 134
  • 319
  • 1
    for your last bullet of point 1 ( _move this.setLocationRelativeTo(null); before this.pack();_ ), I disagree. If the purpose of that call is to center the window on the screen, then `pack()` should definitely be called before, otherwise the `setLocationRelativeTo(null)` will place the top-left corner in the center of the screen since `getSize()` will return (0,0). Calling `pack()` before ensures that the window has the appropriate size. – Guillaume Polet Sep 28 '12 at 16:20
  • Thanks for the reply. But, when the invoke later is given, the window appears after the mail is sent!Please help – PeakGen Sep 28 '12 at 17:23
  • read this thread please http://stackoverflow.com/questions/12641887/swingworker-in-java – mKorbel Sep 28 '12 at 17:29
0

You have created a number of components but have not added them to the JFrame in your application. As you are using GrigBagLayout, you will need to set the constraints for each component, e.g.:

subject = new JLabel("Subject: ");
gbc.fill = GridBagConstraints.BOTH;
gbc.weightx = 1.0;
gbl.setConstraints(subject, gbc);
add(subject);

Also don't forget to instantiate the layout & constraints first:

private GridBagLayout gbl = new GridBagLayout();
private GridBagConstraints gbc = new GridBagConstraints();
Reimeus
  • 158,255
  • 15
  • 216
  • 276