Search code examples
javaswinganimationimap

loading animation during imap connection in java


I am trying to display an loading animation(a gif file) while getting connected and fetching all messages from gmail imap server using java ,but the animation is not visible ...my code is as show below pls help me in this context if any solution exists....

import java.io.*;
import java.awt.*;
import javax.swing.*;
import javax.mail.*;
import java.awt.event.*;
import javax.mail.*;
import javax.mail.event.*;
import java.util.*;



public class vmail extends JFrame implements ActionListener,MouseMotionListener
{
    Container cp;
    JLabel background,loader;
    ImageIcon ic1,ic2;
    JTextField tuname,tpassword;
    JButton blogin;
    JPanel jp_login;

    public vmail()
    {
        setSize(900,640);
        setTitle("GMAIL CLIENT ");
        setLocation(200,50);

        cp=getContentPane();
        cp.setLayout(null);
        cp.setBackground(Color.white);


        String workingDir = System.getProperty("user.dir");
        workingDir=workingDir.substring(0,14);

        loader=new JLabel("");
        ic2=new ImageIcon(workingDir+"\\images\\progressbar1.gif");
        loader.setIcon(ic2);
        loader.setBounds(230,90,400,400);
        loader.setVisible(false);
        cp.add(loader); 



        jp_login=new JPanel();
        jp_login.setLayout(null);








        tuname=new JTextField(10);
        jp_login.add(tuname);
        tuname.setBounds(604,163,245,30);
        tuname.addMouseMotionListener(this);


        tpassword=new JTextField(10);
        jp_login.add(tpassword);
        tpassword.setBounds(604,224,245,30);
        tpassword.addMouseMotionListener(this);



        blogin=new JButton("");

        blogin.setBounds(605,271,53,29);
        blogin.setOpaque(false);
        blogin.setContentAreaFilled(false);
        blogin.setBorderPainted(false);
        blogin.addActionListener(this);
        blogin.addMouseMotionListener(this);
        jp_login.add(blogin);



        background=new JLabel("");

        System.out.println(workingDir);
        ic1=new ImageIcon(workingDir+"\\images\\background.png");
        background.setIcon(ic1);
        background.setBounds(0,0,900,600);

        jp_login.add(background);




        jp_login.setBounds(0,0,900,600);
        cp.add(jp_login);


        setVisible(true);
        setDefaultCloseOperation(3);
    }

    public static void main(String[] args) 
    {
        UIManager.LookAndFeelInfo info[];
        info=UIManager.getInstalledLookAndFeels();
        String name=info[3].getClassName();

        try
        {
            UIManager.setLookAndFeel(name);
        }  
        catch(Exception e)
        {
            System.out.println(e);
        }

        vmail v=new vmail();

    }

    public void actionPerformed(ActionEvent ae)
    {
        if(ae.getSource()==blogin)
        {




            if(tuname.getText().equals("")||tpassword.getText().equals(""))
            {
                JOptionPane.showMessageDialog(this,"Incorrect Data Provided");
            }
            else
            {
                loader.setVisible(true);


                Properties props = System.getProperties();
                props.setProperty("mail.store.protocol", "imaps");
                try 
                {

                        Session session = Session.getDefaultInstance(props, null);
                        Store store = session.getStore("imaps");
                          try
                          {
                            store.connect("imap.gmail.com", tuname.getText(),tpassword.getText());
                            System.out.println("The Store connected is :  "+store);
                          }
                          catch(Exception e)
                          {
                            loader.setVisible(false);
                            JOptionPane.showMessageDialog(this,"Invalid User..");
                            System.out.println("Cannot connect error \n\n"+e);
                          }

                        vmail_in v=new vmail_in(store);
                        loader.setVisible(false);
                        this.setVisible(false);



                }
                catch(Exception e)
                {
                        loader.setVisible(false);
                    System.out.println("Setup Connection Error \n\n"+e);
                }
                loader.setVisible(false);   

            }
        }
    }


    public void mouseMoved(MouseEvent e) 
    {
                    if(e.getSource()==tuname)
                      tuname.setCursor(new Cursor(Cursor.HAND_CURSOR));
                    if(e.getSource()==tpassword)
                      tpassword.setCursor(new Cursor(Cursor.HAND_CURSOR)); 
                    if(e.getSource()==blogin)
                     blogin.setCursor(new Cursor(Cursor.HAND_CURSOR));      

    }

    public void mouseDragged(MouseEvent e) 
        {  }





}

Solution

  • That's because you attempting to establish a connection to your IMAP store within the context of the Event Dispatching Thread (aka EDT).

    One of the responsibilities of the EDT is to dispatch paint requests, so while your blowing it, it can't respond to any repaint updates (or user input).

    You need to use some kind of background thread to execute the connection process, after starting your connection animation.

    I suggest you take a look at Concurrency in Swing and pay special attention to the SwingWorker section