Results 1 to 4 of 4

Thread: Button Actions

  1. #1
    vasavi.singh is offline Member
    Join Date
    Feb 2009
    Posts
    24
    Rep Power
    0

    Default Button Actions

    Hi,

    I created a GUI in which it takes the input and after submitting it has to display the whole content. If the user clicks on the button reset the JTextFields has to be cleared.
    But I am getting the prob while displaying and clearing the data

    <code>

    JButton submit = new JButton("Check");
    JButton clear = new JButton("Reset");

    submit.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent e)
    {
    if("Check".equals(e.getActionCommand()))
    {
    String name=interface1.field1.getText();
    // System.out.println(name);
    // System.out.println("Checking");
    // System.out.println(field2.getText());
    int year=Integer.parseInt(field2.getText());
    int cited=Integer.valueOf(field3.getText()).intValue() ;
    String ref=field4.getText();
    // System.out.println(ref);
    int authorCount=Integer.valueOf(field5.getText()).intV alue();
    interface1.displayArticleInformation(name,year,cit ed,ref,authorCount);
    System.out.println("Checking");
    }
    }
    });

    clear.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent e)
    {
    if("Reset".equals(e.getActionCommand()))
    {
    // System.out.println("Clearing");
    field1.setText("");
    field2.setText("");
    field3.setText("");
    field4.setText("");
    field5.setText(" ");
    System.out.println("Clearing");
    }
    }
    });

    </code>

    On clicking the reset button it is not reseting the fields and while clicking the check button it is raising the exception

    Exception in thread "AWT-EventQueue-0" java.lang.NumberFormatException: For input string: ""
    at java.lang.NumberFormatException.forInputString(Num berFormatException.java:48)
    at java.lang.Integer.parseInt(Integer.java:468)
    at java.lang.Integer.parseInt(Integer.java:497)
    at ArticleInterface$1.actionPerformed(ArticleInterfac e.java:81)
    at javax.swing.AbstractButton.fireActionPerformed(Abs tractButton.java:1849)
    at javax.swing.AbstractButton$Handler.actionPerformed (AbstractButton.java:2169)
    at javax.swing.DefaultButtonModel.fireActionPerformed (DefaultButtonModel.java:420)
    at javax.swing.DefaultButtonModel.setPressed(DefaultB uttonModel.java:258)
    at javax.swing.plaf.basic.BasicButtonListener.mouseRe leased(BasicButtonListener.java:236)
    at java.awt.Component.processMouseEvent(Component.jav a:5517)
    at javax.swing.JComponent.processMouseEvent(JComponen t.java:3135)
    at java.awt.Component.processEvent(Component.java:528 2)
    at java.awt.Container.processEvent(Container.java:196 6)
    at java.awt.Component.dispatchEventImpl(Component.jav a:3984)
    at java.awt.Container.dispatchEventImpl(Container.jav a:2024)
    at java.awt.Component.dispatchEvent(Component.java:38 19)
    at java.awt.LightweightDispatcher.retargetMouseEvent( Container.java:4212)
    at java.awt.LightweightDispatcher.processMouseEvent(C ontainer.java:3892)
    at java.awt.LightweightDispatcher.dispatchEvent(Conta iner.java:3822)
    at java.awt.Container.dispatchEventImpl(Container.jav a:2010)
    at java.awt.Window.dispatchEventImpl(Window.java:1791 )
    at java.awt.Component.dispatchEvent(Component.java:38 19)
    at java.awt.EventQueue.dispatchEvent(EventQueue.java: 463)
    at java.awt.EventDispatchThread.pumpOneEventForHierar chy(EventDispatchThread.java:242)
    at java.awt.EventDispatchThread.pumpEventsForHierarch y(EventDispatchThread.java:163)
    at java.awt.EventDispatchThread.pumpEvents(EventDispa tchThread.java:157)
    at java.awt.EventDispatchThread.pumpEvents(EventDispa tchThread.java:149)
    at java.awt.EventDispatchThread.run(EventDispatchThre ad.java:110)

    the error it is showing at the line where the string is converting to an int.

    On clicking the clear button it is not clearing but the instruction are executing. By putting the println( ) stmt after all setText(), I found that.

    I am not getting wht is the problem, so Please u people help me.
    It is too important and urgent for me to move forward in my project


    regards
    vasavi

  2. #2
    SaSho's Avatar
    SaSho is offline Member
    Join Date
    Dec 2008
    Location
    Ukraine, Dnepropetrovsk
    Posts
    5
    Rep Power
    0

    Default

    Hello
    It's simple.
    Look at this line:
    int year=Integer.parseInt(field2.getText());

    But your field field2 contains empty string. In such case you'll get NumberFormatException. Javadoc says "throws NumberFormatException - if the string does not contain a parsable integer". So, value of filed2 must contains correct representation of integer

  3. #3
    dswastik is offline Senior Member
    Join Date
    Dec 2008
    Location
    Kolkata
    Posts
    282
    Rep Power
    6

    Default

    Most likely its happening when you click on the submit button, and if that is the case are you entering numeric values in the text fields which are being converted to numeric? Probably you need to put to check before using parseInt that String values being converted to numeric are actually numbers.

  4. #4
    OrangeDog's Avatar
    OrangeDog is offline Senior Member
    Join Date
    Jan 2009
    Location
    Cambridge, UK
    Posts
    838
    Rep Power
    6

    Default

    use a try{}catch{} to trap the exception and ignore it (if that is what should happen)

Similar Threads

  1. various actions in menuitem of popupmenu
    By Peggy in forum Java Applets
    Replies: 0
    Last Post: 12-11-2008, 11:50 AM
  2. Showing how to add Actions for KeyStrokes
    By Java Tip in forum javax.swing
    Replies: 0
    Last Post: 04-23-2008, 08:17 PM
  3. Help using Buttons/Actions with Drawings
    By Deathmonger in forum New To Java
    Replies: 0
    Last Post: 04-08-2008, 03:11 PM
  4. Actions
    By alley in forum AWT / Swing
    Replies: 2
    Last Post: 01-16-2008, 02:52 PM
  5. Bean related actions in JSP
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 12-24-2007, 10:04 AM

Posting Permissions

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