Page 1 of 2 12 LastLast
Results 1 to 20 of 22
  1. #1
    Asvin is offline Member
    Join Date
    Nov 2010
    Posts
    66
    Rep Power
    0

    Default Problem with JTextField.. please help!!

    Good afternoon guys! :) I was working on an applet-based java calculator and I have pretty much completed it (or so I think), but I seem to have a problem with the JTextField component in my program. Here is my program:

    Java Code:
    import javax.swing.JApplet;
    import javax.swing.JButton;
    import javax.swing.JLabel;
    import javax.swing.JTextField;
    import java.awt.Container;
    import java.awt.FlowLayout;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    
    public class CalculatorTest extends JApplet implements ActionListener
    {
    
    
    		public static int LINES = 5;
    		public static int CHAR_PER_LINE = 40;
    
    		private JTextField input01;
    		private JTextField input02;
    		private JTextField output01;
    		private char operation;
    		private double input1;
    		private double input2;
    		private double output1;
    
    		public void init()
    		{
    
    
    				Container contentPane = getContentPane();
    				contentPane.setLayout(new FlowLayout());
    
    				JLabel instructions = new JLabel("Enter two inputs and an operation to get the result.");
    				contentPane.add(instructions);
    
    				input01 = new JTextField(LINES, CHAR_PER_LINE);
    				input01.setText(" ");
    				contentPane.add(input01);
    
    				input02 = new JTextField(LINES, CHAR_PER_LINE);
    				input02.setText(" ");
    				contentPane.add(input02);
    
    				output01 = new JTextField(LINES, CHAR_PER_LINE);
    				output01.setText(" ");
    				contentPane.add(output01);
    
    				JButton updateButton = new JButton("Update");
    				updateButton.addActionListener(this);
    				contentPane.add(updateButton);
    
    				JButton resetButton = new JButton("Reset");
    				resetButton.addActionListener(this);
    				contentPane.add(resetButton);
    
    
    
    		}
    
    
    				public void actionPerformed(ActionEvent e)
    				{
    
    
    						String actionCommand = e.getActionCommand();
    						{
    
    						if(actionCommand.equals("Update"))
    						{
    
    
    
    							if(operation=='+')
    							{
    
    							output1 = input1 + input2;
    
    							}
    
    							else if(operation=='-')
    							{
    
    							output1 = input1 - input2;
    							}
    
    							else if(operation=='*')
    							{
    
    							output1 = input1 * input2;
    							}
    
    							else if(operation=='/')
    							{
    
    							output1 = input1 / input2;
    							}
    
    							else
    							output1 = 0;
    						}
    
    						else if(actionCommand.equals("Reset"))
    						{
    
    							input1 = 0;
    							input2 = 0;
    							output1 = 0;
    						}
    
    						else
    						{
    							input1 = 0;
    							input2 = 0;
    							output1 = 0;
    				}
    			}
    	}
    }
    The assignment is to create a calculator program using a java applet. This calculator has an input field and an output field. The user enters two inputs and an operation(+,-,*,/)into the input field in the following format: <input1> <space> <input2> <space> <operation> There are two buttons on this calculator. One is the Update button, which, when clicked, shows the output on the output text field. The other button, reset, clears all text boxes. Any help is much appreciated. Thanks!! :)

  2. #2
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default

    I seem to have a problem with the JTextField component in my program

    What is the problem? Describe both the actual behaviour of your program (at this stage) and the intended or expected behaviour.

  3. #3
    Asvin is offline Member
    Join Date
    Nov 2010
    Posts
    66
    Rep Power
    0

    Default

    The program gives me an error, pointing to the lines where it says

    input01 = new JTextField, input02 = new JTextField and output01 = new JTextField

  4. #4
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,513
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by Asvin View Post
    The program gives me an error, pointing to the lines where it says

    input01 = new JTextField, input02 = new JTextField and output01 = new JTextField
    What was the actual error? A compilation error? A runtime error? What exactly did it say? Please don't make us guess.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  5. #5
    Asvin is offline Member
    Join Date
    Nov 2010
    Posts
    66
    Rep Power
    0

    Default

    I am sorry..

    It was a compilation error.. I used textpad and it won't let me copy the actual error

    It says cannot find symbol and points to the keyword "new" in the lines I mentioned before!

    Thanks for the replies! :)
    Last edited by Asvin; 11-30-2010 at 08:03 PM.

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

    Default

    Quote Originally Posted by Asvin View Post
    I am sorry..

    It was a compilation error.. I used textpad and it won't let me copy the actual error

    It says cannot find symbol and points to the keyword "new" in the lines I mentioned before!

    Thanks for the replies! :)
    So the compiler is telling you that there's no constructor as you've written it. If it were my program I'd go to the JTextField API and check to see if there's a JTextField constructor that takes two ints, my guess is that there isn't. If so, then you will need to use only the constructors that are available.

  7. #7
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default

    Also, it's not clear whether you want a text field or a text area. (Links are to Oracle's Tutorial)

  8. #8
    Asvin is offline Member
    Join Date
    Nov 2010
    Posts
    66
    Rep Power
    0

    Default

    I was looking for a textfield not an area because I want the user to input two inputs and an operation.. then I want to use those ints and the operation to compute a result.. That is where I am stuck at

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

    Default

    Quote Originally Posted by Asvin View Post
    I was looking for a textfield not an area because I want the user to input two inputs and an operation.. then I want to use those ints and the operation to compute a result.. That is where I am stuck at
    Did you at least look at the JTextField API? The tutorial? Its use is all explained there.

  10. #10
    Asvin is offline Member
    Join Date
    Nov 2010
    Posts
    66
    Rep Power
    0

    Default

    Quote Originally Posted by Fubarable View Post
    Did you at least look at the JTextField API? The tutorial? Its use is all explained there.
    Yes I did.. I am still lost though.. :(

  11. #11
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default

    Java Code:
    input01 = new JTextField(LINES, CHAR_PER_LINE);

    Did you understand what was said before about there being no JTextField constructor that takes two int arguments like this?

    What did you expect this line to do? (Not "create a text field", but what sort of text field ... what were those arguments supposed to do?)

    If there is something specific you can't understand with the Tutorial you'll have to say what that is. It's a bit hard for anyone to guess.

  12. #12
    Asvin is offline Member
    Join Date
    Nov 2010
    Posts
    66
    Rep Power
    0

    Default

    Okay guys I fixed up my code a little bit and the only problem I seem to have is getting a numeric value from a textbox.. I declared a "double" type variable and set it equal to input01.getText(); where input01 is the name of my textbox.. Can anyone tell me the right way to get a numeric value from a text box?

  13. #13
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default

    I declared a "double" type variable and set it equal to input01.getText(); where input01 is the name of my textbox.

    What happened when you tried that? If there was a compiler message that you couldn't understand post the code and the message.

    ----------------------

    Basically you don't get a numeric value form a text box, you get text (a string). What you have to do is parse that string to obtain a numeric value. The Double method parseDouble() and the Integer method parseInt() may be useful.

  14. #14
    Asvin is offline Member
    Join Date
    Nov 2010
    Posts
    66
    Rep Power
    0

    Default

    Here is the updated code:

    Java Code:
    import javax.swing.JApplet;
    import javax.swing.JButton;
    import javax.swing.JLabel;
    import javax.swing.JTextField;
    import java.awt.Container;
    import java.awt.FlowLayout;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    
    public class CalculatorTest extends JApplet implements ActionListener
    {
    
    
    		JTextField input01;
    		JTextField input02;
    		JTextField oper01;
    		JTextField output01;
    		char operation;
    		double input1;
    		double input2;
    		double output1;
    
    		public void init()
    		{
    
    
    				Container contentPane = getContentPane();
    				contentPane.setLayout(new FlowLayout());
    
    				JLabel instructions = new JLabel("Enter two inputs and an operation to get the result.");
    				contentPane.add(instructions);
    
    				input01 = new JTextField(" ", 25);
    				input01.setText(" ");
    				contentPane.add(input01);
    
    				input02 = new JTextField(" ", 25);
    				input02.setText(" ");
    				contentPane.add(input02);
    
    				oper01 = new JTextField(" ", 25);
    				oper01.setText(" ");
    				contentPane.add(oper01);
    
    				output01 = new JTextField(" ", 25);
    				output01.setText(" ");
    				contentPane.add(output01);
    
    				JButton updateButton = new JButton("Update");
    				updateButton.addActionListener(this);
    				contentPane.add(updateButton);
    
    				JButton resetButton = new JButton("Reset");
    				resetButton.addActionListener(this);
    				contentPane.add(resetButton);
    
    
    
    		}
    
    
    				public void actionPerformed(ActionEvent e)
    				{
    
    
    						String actionCommand = e.getActionCommand();
    						{
    
    						if(actionCommand.equals("Update"))
    						{
    
    							if(operation=='+')
    							{
    
    							output1 = input1 + input2;
    
    							}
    
    							else if(operation=='-')
    							{
    
    							output1 = input1 - input2;
    							}
    
    							else if(operation=='*')
    							{
    
    							output1 = input1 * input2;
    							}
    
    							else if(operation=='/')
    							{
    
    							output1 = input1 / input2;
    							}
    
    							else
    							output1 = 0;
    						}
    
    						else if(actionCommand.equals("Reset"))
    						{
    
    							input01.setText("");
    							input02.setText("");
    							oper01.setText("");
    							output01.setText("");
    						}
    
    						else
    						{
    
    
    							input01.setText("");
    							input02.setText("");
    							oper01.setText("");
    							output01.setText("");
    				}
    			}
    	}
    }
    i only made some minor changes. I thought it gave me an error while I was compiling. Sorry guys. It works now, but doesn't do what I want it to do. It runs the applet and shows everything and lets me enter values into the 4 textboxes. The first textbox is for input1. The second for input2. The third for the operation(+,-,*,/) and the fourth displays the output. I enter a value into textboxes 1, 2, and 3, but the output box doesn't show anything when I hit update. Any help will be much appreciated and I apologize if I haven't been clear on certain things.. I am new to Java:)

  15. #15
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default

    I enter a value into textboxes 1, 2, and 3, but the output box doesn't show anything when I hit update.

    When the update button is clicked you have code that sets the value of output1 and that's all. You are not going to see anything happen to the text field output01 until you call one of its methods, like setText().

  16. #16
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default

    To elaborate on that a little... In your actionPerformed() you have to do three things:

    (1) Use parseDouble() etc to update the values of input1, input2 and operation which might have changed. Then

    (2) Do some arithmetic as you do now to update the value of output01. Then

    (3) Call setText() on the text field output1 so that it displays the new value of output01

  17. #17
    Asvin is offline Member
    Join Date
    Nov 2010
    Posts
    66
    Rep Power
    0

    Default

    Would the setText() method go within each if statement in the actionPerformed() method or outside?

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

    Default

    Quote Originally Posted by Asvin View Post
    Would the setText() method go within each if statement in the actionPerformed() method or outside?
    That's the beauty of having your own Java compiler and JVM -- you have your own computing laboratory where you can try this out and see for yourself what works and what doesn't work. My lord, what you young whippersnappers take for granted! Why when I was learning to program, we were lucky to be able to get computer (mainframe) time to allow us to compile and run our programs once a day!

    I also used to tie an onion to me belt which was the style at the time. Now, to take the ferry cost a nickel, and in those days, nickels had pictures of bumblebees on 'em. Gimme five bees for a quarter, you'd say. Now where was I... oh yeah. The important thing was that I had an onion tied to my belt, which was the style at the time. You couldn't get white onions, because of the war. The only thing you could get was those big yellow ones...

  19. #19
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,234
    Rep Power
    19

    Default

    we were lucky to be able to get computer (mainframe) time to allow us to compile and run our programs once a day!
    Wow, once a day! you really were lucky!

  20. #20
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,513
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by Fubarable View Post
    I also used to tie an onion to me belt which was the style at the time.
    You had onions? Lucky bastard; we only had lumps of charcoal from the mine where we had to work for 29 hours a day. We had to walk uphill both ways to get there. Our mainframe was steam powered and we had to work for it to keep it running. We were only allowed to use it for half a second or less during the night when we had to work in the mine. We had it rough ...

    kind regards,

    Jos ;-)
    cenosillicaphobia: the fear for an empty beer glass

Page 1 of 2 12 LastLast

Similar Threads

  1. Problem with setColumns in JTextField
    By shai in forum New To Java
    Replies: 3
    Last Post: 07-26-2010, 05:41 AM
  2. Manually position JTextfield problem
    By castiel in forum AWT / Swing
    Replies: 3
    Last Post: 02-20-2010, 05:24 AM
  3. Applet JTextField Size Problem
    By a13w in forum New To Java
    Replies: 4
    Last Post: 09-19-2009, 07:52 AM
  4. Replies: 1
    Last Post: 01-30-2009, 06:44 PM
  5. Got problem with JtextField
    By hungleon88 in forum AWT / Swing
    Replies: 4
    Last Post: 12-06-2008, 03:01 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
  •