Results 1 to 7 of 7
- 06-20-2010, 08:29 PM #1
Member
- Join Date
- Jun 2008
- Posts
- 87
- Rep Power
- 0
JFrame hangs tried additional threading but no use
This is a part of my main code in which i m calling plzwai class which displays alert frame and then new thread which calls sending class which sends the mail to the destination with attachment.
Alert classJava Code:new plzwai(); java.awt.EventQueue.invokeLater(new Runnable() { public void run() { sending s1=new sending(fr,b,s,t,p,ss); result=s1.send(); } }); fplz.setVisible(false);
Java Code:class plzwai extends JFrame implements Runnable { Thread t=new Thread(this); plzwai() { t.start(); } public void run() { fplz=new JFrame("Information"); lplz=new JLabel("Uploading Files Please Wait..... Do not close this application while uploading files as it can harm your computer speed"); fplz.add(new JPanel(),BorderLayout.NORTH); fplz.add(new JPanel(),BorderLayout.SOUTH); fplz.add(new JPanel(),BorderLayout.EAST); fplz.add(new JPanel(),BorderLayout.WEST); fplz.add(lplz,BorderLayout.CENTER); fplz.addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent e) { new plzwai(); } }); fplz.pack(); fplz.setLocationRelativeTo(null); fplz.setVisible(true); } } }
My problem is that when i run this code i get alert JFrame but i can not see any JLabel in it and every thing hangs
please help me
Thank you in advance
- 06-20-2010, 08:36 PM #2
Try debugging the code by adding some println() statements to show where the code is executing and how variables are changing.
Is some method blocking?
- 06-20-2010, 08:54 PM #3
Member
- Join Date
- Jun 2008
- Posts
- 87
- Rep Power
- 0
Here is my complete code
Java Code:import java.awt.*; import java.awt.event.*; import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.FileReader; import java.io.FileWriter; import java.io.IOException; import java.io.LineNumberReader; import java.util.*; import javax.swing.*; public class GUI implements ActionListener { JFrame fplz; JLabel lplz; String ss[]; private Frame f; private Button b1,b2,b3; private TextField to,sub; private TextArea body; private Label l3,l4; private Panel p1,p2,p3,p4,p5,p6; JPanel rav; String fr,s,p,b,result; String[] t; public GUI() { f = new Frame("Send Mail"); f.addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent e) { actionExit(); } }); to = new TextField(70); sub = new TextField(30); body = new TextArea(30, 100); b1 = new Button("Send"); b2 = new Button("Reset"); b3=new Button("Change id"); l3 = new Label("To * "); l4 = new Label("Sub *"); p1 = new Panel(); p2 = new Panel(); p3 = new Panel(); p4 = new Panel(); p5 = new Panel(); rav=new JPanel(new BorderLayout()); p6 = new Panel(new GridLayout(3, 1)); try { launch(); } catch (Exception ex) { System.out.println(ex); } } public int getNumberOfLines(String name) { int numberOfLines = 0; LineNumberReader lineCounter = null; try { lineCounter = new LineNumberReader(new FileReader(name)); while ((lineCounter.readLine()) != null) { continue; } numberOfLines = lineCounter.getLineNumber(); } catch (IOException e) { } return numberOfLines; } public void launch() throws Exception { LookAndFeel lf = UIManager.getLookAndFeel(); try { UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel"); //UIManager.setLookAndFeel("com.sun.java.swing.plaf.motif.MotifLookAndFeel"); } catch (Exception e) { } f.setLayout(new BorderLayout()); p2.add(l3); p2.add(to); p3.add(l4); p3.add(sub); p4.add(body); p5.add(b1); p5.add(b2); p5.add(b3); p6.add(p1); p6.add(p2); p6.add(p3); JButton b4=new JButton("Attachement"); b4.addActionListener(this); p1.add(b4); rav.add(p6,BorderLayout.NORTH); rav.add(p1,BorderLayout.SOUTH); f.add(rav,BorderLayout.NORTH); f.add(p4,BorderLayout.CENTER); f.add(p5,BorderLayout.SOUTH); b1.addActionListener(this); b2.addActionListener(this); b3.addActionListener(this); f.pack(); f.setLocationRelativeTo(null); f.setVisible(true); } private void actionExit() { System.exit(0); } public void actionPerformed(ActionEvent ae) { if(ae.getActionCommand().equals("Send")) { ss=new String[getNumberOfLines("attachment.ravs")]; String data1=null; try { BufferedReader bbb=new BufferedReader(new FileReader("attachment.ravs")); data1=bbb.readLine(); for(int i=0;i<getNumberOfLines("attachment.ravs");i++) { ss[i]=data1; data1=bbb.readLine(); JOptionPane.showMessageDialog(null,ss[i],"Hi",JOptionPane.INFORMATION_MESSAGE); } bbb.close(); }catch(Exception bf){} String add[]=new String[2]; String uu=""; String pp=""; try { int c=0; BufferedReader b=new BufferedReader(new FileReader("Bin/UserInfo/info.ravs")); testing t=new testing("Bin/UserInfo/info.ravs"); String ravsss=t.send(); StringTokenizer r=new StringTokenizer(ravsss,"\n"); int cc=0; while(r.hasMoreTokens()) { add[cc]=r.nextToken(); cc++; } /*String data=b.readLine(); while(data!=null) { if(c==0) { uu=data; c++; } else { pp=data; } data=b.readLine(); } b.close();*/ }catch(Exception e21){} fr=add[0]; p=add[1]; s=sub.getText(); b=body.getText(); b=b+"\n\n\n\n "+"Rav Softs. Presentation"; StringTokenizer st1=new StringTokenizer(to.getText(),","); t=new String[st1.countTokens()]; int i=0; while(st1.hasMoreTokens()) { t[i]=st1.nextToken(); i++; } //lab.setText("Please Wait.."); new plzwai(); java.awt.EventQueue.invokeLater(new Runnable() { public void run() { sending s1=new sending(fr,b,s,t,p,ss); result=s1.send(); } }); fplz.setVisible(false); try { BufferedWriter ba=new BufferedWriter(new FileWriter("att.ravs")); BufferedWriter ba1=new BufferedWriter(new FileWriter("attachment.ravs")); ba.append(""); ba1.append(""); ba.close(); ba1.close(); }catch(Exception ada){} JOptionPane.showMessageDialog(null, result,"Alert",JOptionPane.INFORMATION_MESSAGE); if(result.equals("Your message is successfully mailed")) { f.setVisible(false); new GUI(); } } else if(ae.getActionCommand().equalsIgnoreCase("Change id")) { f.setVisible(false); new First(0); } else if(ae.getActionCommand().equalsIgnoreCase("attachement")) { new attachement(); } else { to.setText(""); sub.setText(""); body.setText(""); } } class plzwai extends JFrame implements Runnable { Thread t=new Thread(this); plzwai() { t.start(); } public void run() { fplz=new JFrame("Information"); lplz=new JLabel("Uploading Files Please Wait..... Do not close this application while uploading files as it can harm your computer speed"); fplz.add(new JPanel(),BorderLayout.NORTH); fplz.add(new JPanel(),BorderLayout.SOUTH); fplz.add(new JPanel(),BorderLayout.EAST); fplz.add(new JPanel(),BorderLayout.WEST); fplz.add(lplz,BorderLayout.CENTER); fplz.addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent e) { new plzwai(); } }); fplz.pack(); fplz.setLocationRelativeTo(null); fplz.setVisible(true); } } }Java Code:import java.security.Security; import java.util.Properties; import javax.activation.*; import javax.mail.*; import javax.mail.internet.*; public class sending { private static final String SMTP_HOST_NAME = "smtp.gmail.com"; private static final String SMTP_PORT = "465"; private static final String SSL_FACTORY = "javax.net.ssl.SSLSocketFactory"; String MsgTxt=null; String Subject=null; String From=null; String pwd=null; String[] too; String[] sss; static int count=0; public sending(String fr,String msg,String sub,String[] fro,String p,String[] ss) { MsgTxt=msg; Subject=sub; From=fr; pwd=p; too=fro; sss=ss; send(); } public String send() { String s=""; Security.addProvider(new com.sun.net.ssl.internal.ssl.Provider()); try { sendSSLMessage(too,Subject,MsgTxt,From,pwd,sss); }catch(Exception e) { s=e.getMessage(); } if(s.equals("")) { s="Your message is successfully mailed"; } return(s); } public void sendSSLMessage(String[] recipients, String subject,String message, String from,String pwd,String[] ssss) throws MessagingException { boolean debug = true; Properties props = new Properties(); props.put("mail.smtp.host", SMTP_HOST_NAME); props.put("mail.smtp.auth", "true"); props.put("mail.debug", "true"); props.put("mail.smtp.port", SMTP_PORT); props.put("mail.smtp.socketFactory.port", SMTP_PORT); props.put("mail.smtp.socketFactory.class", SSL_FACTORY); props.put("mail.smtp.socketFactory.fallback", "false"); final String from1=from; final String pwd1=pwd; Session session = Session.getDefaultInstance(props,new javax.mail.Authenticator() {protected PasswordAuthentication getPasswordAuthentication() {return new PasswordAuthentication(from1,pwd1);}}); session.setDebug(debug); MimeMessage msg = new MimeMessage(session); InternetAddress addressFrom = new InternetAddress(from); msg.setFrom(addressFrom); InternetAddress[] addressTo = new InternetAddress[recipients.length]; for (int i = 0; i <recipients.length; i++) { addressTo[i] = new InternetAddress(recipients[i]); } msg.setRecipients(Message.RecipientType.TO, addressTo); msg.setSubject(subject); MimeBodyPart mbp1 = new MimeBodyPart ( ) ; mbp1.setText ( message ) ; MimeBodyPart attachment[]=new MimeBodyPart[sss.length]; for(int i=0;i<sss.length;i++) { attachment[i] = new MimeBodyPart() ; FileDataSource fds = new FileDataSource ( sss[i] ) ; attachment[i].setDataHandler ( new DataHandler ( fds ) ) ; attachment[i].setFileName ( fds.getName() ) ; } Multipart mp = new MimeMultipart ( ) ; mp.addBodyPart ( mbp1 ) ; for(int j=0;j<sss.length;j++) { mp.addBodyPart ( attachment[j] ) ; } // msg.setContent(message, "text/plain"); msg.setContent(mp); Transport.send(msg); from=null; recipients=null; pwd=null; subject=null; message=null; } }
- 06-20-2010, 09:30 PM #4
Have you tried debugging it yet? What can I do looking at your code? You have to execute it to test it.
I only see one println() statement in a catch block which is a poor technique. You should use printStackTrace() to show where and what happened for an exception.
- 06-20-2010, 09:43 PM #5
Senior Member
- Join Date
- May 2010
- Posts
- 436
- Rep Power
- 4
There's little doubt in my mind that you have a Swing threading issue. If you haven't done so, you should study this trail: Concurrency in Swing
- 06-20-2010, 11:17 PM #6
Try creating a background thread instead of using the EventQueue.
You have code following where you start the thread that will be executed before the thread starts. Move that code into the run() method.
- 06-21-2010, 06:01 AM #7
Member
- Join Date
- Jun 2008
- Posts
- 87
- Rep Power
- 0
Similar Threads
-
Print image and send additional PCL instructions.
By bronai in forum Advanced JavaReplies: 1Last Post: 05-16-2011, 07:18 PM -
Additional Weight
By laserjim in forum LuceneReplies: 0Last Post: 12-13-2009, 01:22 AM -
Program hangs
By yuliada in forum Advanced JavaReplies: 2Last Post: 11-11-2009, 03:58 AM -
Applet hangs while using JOptionPane.xxx()
By Basit in forum Java AppletsReplies: 3Last Post: 08-08-2009, 06:18 PM -
Discussion: JVM hangs
By Arica in forum Advanced JavaReplies: 5Last Post: 06-29-2009, 04:29 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks