Results 1 to 10 of 10
  1. #1
    thelinuxguy is offline Member
    Join Date
    Dec 2008
    Posts
    44
    Rep Power
    0

    Default 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

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    29

    Default

    did you ever figure out your TextListener issue?

  3. #3
    thelinuxguy is offline Member
    Join Date
    Dec 2008
    Posts
    44
    Rep Power
    0

    Default 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.

  4. #4
    StormyWaters is offline Senior Member
    Join Date
    Feb 2009
    Posts
    312
    Rep Power
    10

    Default

    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
    }

  5. #5
    thelinuxguy is offline Member
    Join Date
    Dec 2008
    Posts
    44
    Rep Power
    0

    Default 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

  6. #6
    thelinuxguy is offline Member
    Join Date
    Dec 2008
    Posts
    44
    Rep Power
    0

    Default 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

  7. #7
    CJSLMAN's Avatar
    CJSLMAN is offline Moderator
    Join Date
    Oct 2008
    Location
    Mexico
    Posts
    1,159
    Rep Power
    11

    Default You can also used the methods...

    Read following post...
    Last edited by CJSLMAN; 02-20-2009 at 02:49 AM. Reason: Accidently posted the same thing twice
    Chris S.
    Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.

  8. #8
    CJSLMAN's Avatar
    CJSLMAN is offline Moderator
    Join Date
    Oct 2008
    Location
    Mexico
    Posts
    1,159
    Rep Power
    11

    Default 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:

    Java Code:
    otherAmount.append(String.valueOf(val));
    Like I said Stormy's suggestion works (maybe a little hard to read), but using the correct methods is the perfered way.

    Luck,
    CJSL
    Chris S.
    Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.

  9. #9
    thelinuxguy is offline Member
    Join Date
    Dec 2008
    Posts
    44
    Rep Power
    0

    Default 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

  10. #10
    thelinuxguy is offline Member
    Join Date
    Dec 2008
    Posts
    44
    Rep Power
    0

    Default displayArea.append(String.valueOf(val));

    Hi

    displayArea.append(String.valueOf(val));

    works perfectly!

Similar Threads

  1. Some advice please!
    By awebbtt in forum New To Java
    Replies: 3
    Last Post: 02-02-2009, 08:23 PM
  2. Replies: 1
    Last Post: 01-30-2009, 07:44 PM
  3. printing two smallest numbers from a series of numbers
    By trofyscarz in forum New To Java
    Replies: 2
    Last Post: 10-15-2008, 12:46 AM
  4. Advice on best method for....
    By shaungoater in forum Java 2D
    Replies: 1
    Last Post: 06-23-2008, 08:36 PM
  5. Replies: 2
    Last Post: 03-30-2008, 09:38 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
  •