Results 1 to 5 of 5
  1. #1
    jesicapalma is offline Member
    Join Date
    Mar 2008
    Posts
    1
    Rep Power
    0

    Question jTextField Simpe Addition - how to ? - Netbeans 6.0

    Hello everybody,

    i have a problem that for many people who know Java may 'll seems funny .

    In NetBeans 6.0, i have three jTextFields and one Button1 .

    jTextField1, jTextField2, jTextField3, Button1 .

    to the jTextField1 and jTextField2 i gave for text value the number of 30 so that we have that jTextField1 =30 and jTextField2 = 30 .

    I want when i click on the button1 (jButton1MouseClicked) addition to start and to get the sum of the first and second jTextField, so 30+30 = 60 .

    This 60, i want to be written into jTextField3 .

    I tried lot of ways but no success :( :confused:

    ..please help ;)

    waiting for your answers , thankx in advance !

  2. #2
    sylvpan's Avatar
    sylvpan is offline Member
    Join Date
    Mar 2008
    Location
    beijing,China
    Posts
    5
    Rep Power
    0

    Thumbs up you can handled this by javascript

    <script>
    function button_click(){
    var text1 = document.getElmentById('txt1');
    var value1 = text1.value.parseInt();
    var text2 = document.getElmentById('txt1');
    var value2 = text2.value.parseInt();
    var text3 = document.getElmentById('txt2');
    text3.value = value1+value2;
    }
    </script>
    I hope this answer can fit for you requirement!

  3. #3
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default


    Welcome jesicapalma... to Java Forums.

    If you are working on Netbeans it is much easy. You no need to worry about the design of GUI at all. I think it is ok for you.

    As you said add three jTextFields and a button. To get the button event simply double-click on the button. You may comes with a method like this.

    Java Code:
        private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
            // TODO add your handling code here:
        }
    Then move to the functionality. Simply add following code segment in that method.

    Java Code:
            int valueOne = Integer.parseInt(jTextField1.getText());
            int valueTwo = Integer.parseInt(jTextField2.getText());
            
            int sum = valueOne + valueTwo;
            
            jTextField3.setText(String.valueOf(sum));
    Hope it is help to you.

  4. #4
    nv.pratha is offline Member
    Join Date
    May 2010
    Posts
    1
    Rep Power
    0

    Default

    I did the same. but error occurs while printing
    Exception in thread "AWT-EventQueue-0" java.lang.NumberFormatException: For input string: " 2"
    at java.lang.NumberFormatException.forInputString(Num berFormatException.java:48)
    at java.lang.Integer.parseInt(Integer.java:447)
    at java.lang.Integer.parseInt(Integer.java:497)
    at NewJFrame.jButton1ActionPerformed(NewJFrame.java:9 7)
    at NewJFrame.access$000(NewJFrame.java:11)
    at NewJFrame$1.actionPerformed(NewJFrame.java:40)
    at javax.swing.AbstractButton.fireActionPerformed(Abs tractButton.java:1995)
    at javax.swing.AbstractButton$Handler.actionPerformed (AbstractButton.java:2318)
    at javax.swing.DefaultButtonModel.fireActionPerformed (DefaultButtonModel.java:387)
    at javax.swing.DefaultButtonModel.setPressed(DefaultB uttonModel.java:242)
    at javax.swing.plaf.basic.BasicButtonListener.mouseRe leased(BasicButtonListener.java:236)
    at java.awt.Component.processMouseEvent(Component.jav a:6038)
    at javax.swing.JComponent.processMouseEvent(JComponen t.java:3260)
    at java.awt.Component.processEvent(Component.java:580 3)
    at java.awt.Container.processEvent(Container.java:205 8)
    at java.awt.Component.dispatchEventImpl(Component.jav a:4410)
    at java.awt.Container.dispatchEventImpl(Container.jav a:2116)
    at java.awt.Component.dispatchEvent(Component.java:42 40)
    at java.awt.LightweightDispatcher.retargetMouseEvent( Container.java:4322)
    at java.awt.LightweightDispatcher.processMouseEvent(C ontainer.java:3986)
    at java.awt.LightweightDispatcher.dispatchEvent(Conta iner.java:3916)
    at java.awt.Container.dispatchEventImpl(Container.jav a:2102)
    at java.awt.Window.dispatchEventImpl(Window.java:2429 )
    at java.awt.Component.dispatchEvent(Component.java:42 40)
    at java.awt.EventQueue.dispatchEvent(EventQueue.java: 599)
    at java.awt.EventDispatchThread.pumpOneEventForFilter s(EventDispatchThread.java:273)
    at java.awt.EventDispatchThread.pumpEventsForFilter(E ventDispatchThread.java:183)
    at java.awt.EventDispatchThread.pumpEventsForHierarch y(EventDispatchThread.java:173)
    at java.awt.EventDispatchThread.pumpEvents(EventDispa tchThread.java:168)
    at java.awt.EventDispatchThread.pumpEvents(EventDispa tchThread.java:160)
    at java.awt.EventDispatchThread.run(EventDispatchThre ad.java:121)

    Can u please find me a solution??

  5. #5
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    7

    Default

    Looks like you have a space before your 2. Better trim the string before parsing it.

Similar Threads

  1. how to focus to another JTextfield?
    By birdofprey in forum AWT / Swing
    Replies: 2
    Last Post: 04-09-2008, 01:08 PM
  2. Replies: 0
    Last Post: 03-28-2008, 08:46 PM
  3. Using Columns With JTextField
    By The Evil Genius in forum AWT / Swing
    Replies: 1
    Last Post: 03-17-2008, 01:01 AM
  4. help with JTextfield
    By gary in forum New To Java
    Replies: 4
    Last Post: 07-11-2007, 01:58 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
  •