Results 1 to 10 of 10
Thread: JTextField add numbers advice
- 02-19-2009, 06:14 PM #1
Member
- Join Date
- Dec 2008
- Posts
- 44
- Rep Power
- 0
JTextField add numbers advice
Hi
I am working on an atm system. So far I have generally been able to use buttons for selections going through a menu driven system just fine. I have an option which involves the user entering an amount that they would like to withdraw. I have a keypad which shows 12 buttons , 0,1,2,3,4,5,6,7,8,9 ,yes,no. The buttons have been implemented in order to allow the input of the numbers that will form the amount that the user would like to withdraw. I have an issue however. The JTextfield append, setText, getText methods only deal with Strings. I am attempting to add whole numbers. Is there a method by which I can encourage the JTextField methods to take in integers.
I have been trying :-
if(source == i)
{
int val = 1;
otherAmount.append(val);
}
and getting the error message
symbol : method append(int)
location: class javax.swing.JTextField
otherAmount.append(val);
Any help appreciated
TheLinuxGuy
-
did you ever figure out your TextListener issue?
- 02-19-2009, 06:44 PM #3
Member
- Join Date
- Dec 2008
- Posts
- 44
- Rep Power
- 0
TextListener
Hi Fubarable
No sorry I have still not sorted out the TextArea issue. I was leaving it and hoping to just use JtextFields and JButtons for the input and then the JTextArea, soley for display purposes. It seems to be working ok. Although I may take a further look at it later. I believe you were right, the TextListener was awt and not swing.
Thanks for asking
TheLinuxGuy
p.s With regard to my current post, I was just doing some searching an have found an earlier post;-
how to display a sum of all previously pressed numbers in JTextField
which has pretty much resolved my problem. I just use Integer.toString in order to convert my integer to a String. I think it will work. Will post if not.
- 02-19-2009, 08:46 PM #4
Senior Member
- Join Date
- Feb 2009
- Posts
- 303
- Rep Power
- 5
Are you just appending the number clicked to the JTextField? In that case why would you need to convert it to a Integer until ok is clicked?
if(source == i)
{
int val = 1;
otherAmount.append(val + ""); //This will convert the int value to a String
}
- 02-20-2009, 01:13 AM #5
Member
- Join Date
- Dec 2008
- Posts
- 44
- Rep Power
- 0
JTextField appending
Hi Stormy Waters
Thanks for your reply, sorry for the late response I, the area had a power cut. I have tried the snippet that you gave me and it works. What I am trying to make is similar to the windows calculator, where if you click 1 then 1 is added to the screen, then if you click 2 the same happens and so fourth for each number. I then hope to take the figure entered in order to use it as a variable value which I can then check against the content of a database, this is for an atm system.
As mentioned the snippet that you gave me has overcome the problem of entering a value into the textfield, I will now try and add further numbers.
Thanks
theLinuxGuy
- 02-20-2009, 01:48 AM #6
Member
- Join Date
- Dec 2008
- Posts
- 44
- Rep Power
- 0
JTextField/JTextArea
Hi Fubarable
In regard to TextListeners I have not researched these further. However I think it may be possible to overcome the problem I was having with the use of a code snippet that StormyWaters has given me. Rather than using JTextFields I am able to use JTextAreas as I initially intended, as with the use of:-
otherAmount.append(val + "");
I am able to add values without an error being displayed.
theLinuxGuy
- 02-20-2009, 01:48 AM #7
You can also used the methods...
Read following post...
Last edited by CJSLMAN; 02-20-2009 at 01:49 AM. Reason: Accidently posted the same thing twice
Chris S.
Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.
- 02-20-2009, 01:49 AM #8
You can also used the methods...
Just a brief comment here... what Stromy suggested works (quick and dirty), but the the correct and formal way would be:
Like I said Stormy's suggestion works (maybe a little hard to read), but using the correct methods is the perfered way.Java Code:otherAmount.append(String.valueOf(val));
Luck,
CJSLChris S.
Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.
- 02-20-2009, 02:01 AM #9
Member
- Join Date
- Dec 2008
- Posts
- 44
- Rep Power
- 0
Appending to JTextArea, setting String as Integer
hi CJSLMAN
Thank you for the code snippet and I will try it in just a minute. I have managed with the help of the code provided by StormyWaters to do what I was intending to do. I have entered numbers into a JtextArea, and then read these numbers as a String using the getText method. I then had to take the String an turn it into an integer to initialise a variable for further use. I found a very useful snippet on another site:- Converting String to Integer - bytes, which has showed me how to do this. For anyones interest in order to take a String and turn it into an integer use the following syntax.
int i = Integer.parseInt("123");
or in my case it was:-
String get = displayArea.getText();
int geti = Integer.parseInt(get);
This gave me the desired outcome.
Regards
theLinuxGuy
- 02-20-2009, 02:03 AM #10
Member
- Join Date
- Dec 2008
- Posts
- 44
- Rep Power
- 0
Similar Threads
-
Some advice please!
By awebbtt in forum New To JavaReplies: 3Last Post: 02-02-2009, 07:23 PM -
how to access jTextField of one JFrame1 from JFrame2 & Modify JTextField contents
By sumit1mca in forum AWT / SwingReplies: 1Last Post: 01-30-2009, 06:44 PM -
printing two smallest numbers from a series of numbers
By trofyscarz in forum New To JavaReplies: 2Last Post: 10-14-2008, 11:46 PM -
Advice on best method for....
By shaungoater in forum Java 2DReplies: 1Last Post: 06-23-2008, 07:36 PM -
how to display a sum of all previously pressed numbers in JTextField?
By all eyes in forum New To JavaReplies: 2Last Post: 03-30-2008, 08:38 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks