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

    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
    Rep Power

    Thumbs up you can handled this by javascript

    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;
    I hope this answer can fit for you requirement!

  3. #3
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Colombo, Sri Lanka
    Blog Entries
    Rep Power


    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;
    Hope it is help to you.

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


    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
    at java.lang.Integer.parseInt(
    at java.lang.Integer.parseInt(
    at NewJFrame.jButton1ActionPerformed( 7)
    at NewJFrame.access$000(
    at NewJFrame$1.actionPerformed(
    at javax.swing.AbstractButton.fireActionPerformed(Abs
    at javax.swing.AbstractButton$Handler.actionPerformed (
    at javax.swing.DefaultButtonModel.fireActionPerformed (
    at javax.swing.DefaultButtonModel.setPressed(DefaultB
    at javax.swing.plaf.basic.BasicButtonListener.mouseRe leased(
    at java.awt.Component.processMouseEvent(Component.jav a:6038)
    at javax.swing.JComponent.processMouseEvent(JComponen
    at java.awt.Component.processEvent( 3)
    at java.awt.Container.processEvent( 8)
    at java.awt.Component.dispatchEventImpl(Component.jav a:4410)
    at java.awt.Container.dispatchEventImpl(Container.jav a:2116)
    at java.awt.Component.dispatchEvent( 40)
    at java.awt.LightweightDispatcher.retargetMouseEvent(
    at java.awt.LightweightDispatcher.processMouseEvent(C
    at java.awt.LightweightDispatcher.dispatchEvent(Conta
    at java.awt.Container.dispatchEventImpl(Container.jav a:2102)
    at java.awt.Window.dispatchEventImpl( )
    at java.awt.Component.dispatchEvent( 40)
    at java.awt.EventQueue.dispatchEvent( 599)
    at java.awt.EventDispatchThread.pumpOneEventForFilter s(
    at java.awt.EventDispatchThread.pumpEventsForFilter(E
    at java.awt.EventDispatchThread.pumpEventsForHierarch y(
    at java.awt.EventDispatchThread.pumpEvents(EventDispa
    at java.awt.EventDispatchThread.pumpEvents(EventDispa

    Can u please find me a solution??

  5. #5
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Rep Power


    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