Results 1 to 9 of 9
  1. #1
    romanzo is offline Member
    Join Date
    Feb 2012
    Posts
    3
    Rep Power
    0

    Default Computing Values in TextField

    Hi,

    I need help to compute the values on a TextField by converting it into Int,
    this is the code and I got no other clues on doing this...
    or post a link that has the answers tnx..

    Java Code:
    import java.io.*;
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    public class advjav extends JFrame{
    	double add,a,b;
    	JButton compute=new JButton("COMPUTE");
    	JTextField tf=new JTextField();
    	JTextField tf2=new JTextField();
    	JLabel jl=new JLabel("Input first No.");
    	JLabel jl2=new JLabel("Input second No.");
    	JPanel jp=new JPanel(new GridLayout(4,1));
    
    
    	public advjav()throws Exception{
    		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		jp.add(jl);
    		jp.add(tf);
    		jp.add(jl2);
    		jp.add(tf2);
    		jp.add(compute);
    
    		compute.addActionListener(new ActionListener(){
    					public void actionPerformed(ActionEvent e){
    						try{
    				String i=tf.getText();
    				int s=Integer.valueOf(i);
    				tf.setText(i);
    				String b=tf2.getText();
    				int c=Integer.valueOf(b);
    				tf2.setText(b);
    				System.out.print(""+(i+b));
    			}catch(Exception exp){}
    				}
    					});
    
    		getContentPane().add(jp, "Center");
    		pack();
    		}
    
    		public static void main(String [] args)throws Exception{
    		new advjav().setVisible(true);
    		}
    }
    Last edited by romanzo; 02-09-2012 at 10:11 AM.

  2. #2
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,091
    Rep Power
    20

    Default Re: Computing Values in TextField

    Java Code:
    import java.io.*;
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    public class advjav extends JFrame{
    	double add,a,b;
    	JButton compute=new JButton("COMPUTE");
    	JTextField tf=new JTextField();
    	JTextField tf2=new JTextField();
    	JLabel jl=new JLabel("Input first No.");
    	JLabel jl2=new JLabel("Input second No.");
    	JPanel jp=new JPanel(new GridLayout(4,1));
    
    
    	public advjav()throws Exception{
    		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		jp.add(jl);
    		jp.add(tf);
    		jp.add(jl2);
    		jp.add(tf2);
    		jp.add(compute);
    
    		compute.addActionListener(new ActionListener(){
    					public void actionPerformed(ActionEvent e){
    						try{
    				String i=tf.getText();
    				int s=Integer.valueOf(i);
    				tf.setText(i);
    				String b=tf2.getText();
    				int c=Integer.valueOf(b);
    				tf2.setText(b);
    				System.out.print(""+(i+b));
    			}catch(Exception exp){}
    				}
    					});
    
    		getContentPane().add(jp, "Center");
    		pack();
    		}
    
    		public static void main(String [] args)throws Exception{
    		new advjav().setVisible(true);
    		}
    }
    Please use code tags when posting code.

    Are you getting any errors?
    If so what are they and what lines are causing them?

    People aren't going to run this to see what the problem is.

  3. #3
    romanzo is offline Member
    Join Date
    Feb 2012
    Posts
    3
    Rep Power
    0

    Default Re: Computing Values in TextField

    Sorry about the tags but there are no run time error i just want to know what codes to use to convert the string value on the TextField to Integer.

  4. #4
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,091
    Rep Power
    20

    Default Re: Computing Values in TextField

    Have a look at the API for Integer.

  5. #5
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,091
    Rep Power
    20

    Default Re: Computing Values in TextField

    Oh yes, one thing, don't do this:
    Java Code:
    }catch(Exception exp){}
    Because if you get an exception you'll never know about it.
    At the least do this:
    Java Code:
    } catch(Exception exp) {
        exp.printStackTrace();
    }

  6. #6
    romanzo is offline Member
    Join Date
    Feb 2012
    Posts
    3
    Rep Power
    0

    Default Re: Computing Values in TextField

    thanks for the help tolls...

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

    Default Re: Computing Values in TextField

    Nothing advanced about this question. Moving to New to Java

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  8. #8
    Kuttappu's Avatar
    Kuttappu is offline Member
    Join Date
    Aug 2011
    Location
    Kerala, India
    Posts
    40
    Rep Power
    0

    Default Re: Computing Values in TextField

    I dont know what i say is foolishness coz this is something basic. Have you tried this?

    String s = tf.getText();
    int a = Integer.parseInt(s);

  9. #9
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,091
    Rep Power
    20

    Default Re: Computing Values in TextField

    Did you miss my post?
    That's exactly what I was pointing the OP at with the API link.
    That way they also get to know about where the API is and how to look for things in it.

Similar Threads

  1. Computing bytes
    By captainjack in forum New To Java
    Replies: 5
    Last Post: 10-28-2011, 01:22 AM
  2. Allowing only hex values in a textfield
    By elsanthosh in forum AWT / Swing
    Replies: 3
    Last Post: 07-20-2010, 09:31 AM
  3. hex values in textfield
    By elsanthosh in forum AWT / Swing
    Replies: 2
    Last Post: 07-19-2010, 01:43 PM
  4. Computing the sum
    By Limuh in forum New To Java
    Replies: 3
    Last Post: 08-13-2008, 06:00 AM
  5. Allowing only numeric values in a TextField
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 03-01-2008, 10:08 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
  •