Results 1 to 8 of 8
  1. #1
    BUGSIE91 is offline Member
    Join Date
    Oct 2010
    Posts
    5
    Rep Power
    0

    Unhappy Multithreading Gui

    frns,please help me with this code....it shows some exception.i am new to java and i dunno how to solve this. :(



    Java Code:
    import java.awt.*;
    import java.awt.event.*;//for specifying windows events
    import javax.swing.*;
    import java.lang.*;
    public class FrontEn implements ActionListener,Runnable
    {
    JTextField jav,it,c;
    Frame f;
    JButton Submit;
    int aa,bb,cc,tot;
    float avg,x;
    FrontEn()                                                    //Constructor that has the basic GUI code...
    {
    f=new Frame("Progress Calculator");
    JLabel l1=new JLabel("Enter the marks here:");
    f.add(l1);
    Panel p=new Panel();
    Panel p1=new Panel();
    JLabel java=new JLabel("Java");
    jav=new JTextField(3);
    System.out.println(" ");
    JLabel itc=new JLabel("Information Theorey");
    it=new JTextField(3);
    JLabel cn=new JLabel("Computer Networks");
    c=new JTextField(3);
    p.setLayout(new GridLayout(3,1));
    p.add(java);
    p.add(itc);
    p.add(cn);
    p.add(jav);
    p.add(it);
    p.add(c);
    Submit=new JButton("Submit");
    p.add(Submit);
    p1.add(p);
    f.add(p1,BorderLayout.CENTER);
    f.setSize(500,300);
    f.setVisible(true);
                                                                     //assigning the values given in the textfield to the integer variables declared in claSS
    aa=Integer.parseInt(jav.getText());
    bb=Integer.parseInt(it.getText());
    cc=Integer.parseInt(c.getText());
    Submit.addActionListener(this); 
                                                                     //creating the thread by implementing Runnable
    Thread T=new Thread(this);
    T.start();
    }
    
    public void actionPerformed(ActionEvent ae) 
    {
             
    String str = ae.getActionCommand();
     
                                                                                    //To check if the Submit button is pressed or not
    if(str.equals("Submit"))
    
    {
                                                                                                 //defining wat shud b done afte the button is pressed
    f.setVisible(false);
    tot=aa+bb+cc;
    JFrame f2=new JFrame("Result");
    f2.addWindowListener(new WindowAdapter(){
    public void windowClosing(WindowEvent e){
    System.exit(0);
    }});
    JLabel Total=new JLabel("TOTAL:");
    JLabel t=new JLabel(Float.toString(tot));
    JLabel avv=new JLabel("AVERAGE");
    JLabel av=new JLabel(Float.toString(x));
    f2.setLayout(new GridLayout(3,1));
    f2.add(Total);
    f2.add(t);
    f2.add(avv);
    f2.add(av);
    f2.setSize(300,300);
    f2.setVisible(true);
    }
    }
                                                               //Thread's RUN function
    public void run()
    {
    try{
    avg=(aa+bb+cc)/3;
    x=avg;}
    catch(Exception e)
    {
    }
    }
    public static void main(String ar[])
    {
    new FrontEn();
    }
    }

    When i tried executing,it showed following error...





    G:\javaessentials>javac FrontEn.java

    G:\javaessentials>java FrontEn

    Exception in thread "main" java.lang.NumberFormatException: For input string: ""

    at java.lang.NumberFormatException.forInputString(Unk nown Source)
    at java.lang.Integer.parseInt(Unknown Source)
    at java.lang.Integer.parseInt(Unknown Source)
    at FrontEn.<init>(FrontEn.java:39)
    at FrontEn.main(FrontEn.java:88)


    pls help me....
    Last edited by BUGSIE91; 10-13-2010 at 02:39 PM.

  2. #2
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,458
    Rep Power
    20

    Default

    I don't read unformatted code, but the error you posted has nothing to do with either multithreading or a GUI.

    What number would you expect to result out of parsing the String "" (aka the empty String)?

    db

  3. #3
    BUGSIE91 is offline Member
    Join Date
    Oct 2010
    Posts
    5
    Rep Power
    0

    Default

    this app actually gets 3 numbers...adds and gives the total and then the average is caluclated...so the unknown source indicates at the numbers that are to b given as input....i need to get the inputs via textfields....but wen i run,i get this error...

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

  5. #5
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    You're trying to parse the JTextField on application creation, before the user has entered any data. You should

    1) Do all your parsing in the actionPerformed method so that it occurs after data has been entered.
    2) Do not mix AWT and Swing components but rather use all Swing components (e.g., use a JFrame not a Frame).
    3) Next time, please only post formatted code. If you make it easier for us to read your post, more will help you quicker.
    4) Next time show which line is causing the exception (the exception gives line numbers but this is only valid in your actual source file, not in the code we see posted above).

    Luck.

  6. #6
    BUGSIE91 is offline Member
    Join Date
    Oct 2010
    Posts
    5
    Rep Power
    0

    Default

    i dunno what u mean by formatting...sorry...i shud add lot of comment line ah???

  7. #7
    BUGSIE91 is offline Member
    Join Date
    Oct 2010
    Posts
    5
    Rep Power
    0

    Default

    @Fubarable sir,i changed the code as u said....now the statements in the Run() of the thread is not working :| ...i am getting the total,which is displayed in the actionListener function and not the average tat is calculated by the thread

  8. #8
    BUGSIE91 is offline Member
    Join Date
    Oct 2010
    Posts
    5
    Rep Power
    0

    Default

    thanks so much....i finally finished the program....i coded the GUI part seperatly for the run() program...so when the program ran,i entered the numbers and total was shown by a frame and average was shown seperatly by another frame.....thanks so much for helping me out :D

Similar Threads

  1. Want to know about Multithreading.
    By Chetans in forum Threads and Synchronization
    Replies: 1
    Last Post: 03-19-2010, 08:50 AM
  2. Multithreading in java
    By MuslimCoder in forum New To Java
    Replies: 5
    Last Post: 02-20-2010, 09:16 PM
  3. Log 4j Multithreading
    By joe2010 in forum Threads and Synchronization
    Replies: 1
    Last Post: 01-31-2010, 04:48 AM
  4. multithreading
    By shilpa.krishna in forum New To Java
    Replies: 2
    Last Post: 06-27-2008, 05:18 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •