Results 1 to 5 of 5
  1. #1
    Jcbconway is offline Senior Member
    Join Date
    Sep 2010
    Posts
    109
    Rep Power
    0

    Exclamation Java JTextArea/Field and String Help Needed!

    Hello, i have a calculator program made with a JFrame with 2 JPanels, 1 panel has all the buttons and the second JPanel has my JTextArea. so, everything works with my buttons and when i press one it displays that number int the JTextArea, but when i press another number it takes the previous number off the text area and displays the new one.
    Java Code:
    if (e.getSource() == b1) { 
                    number1 = (n7 + prevnum);
                    System.out.print(number1);
                    TF.setText(number1);
                }
    prevnum is the string of numbers before the new number added to the string and by defualt it is set to "" nothing.
    n7 is the string "7".
    TF is the veriable for my textArea (but i have also tried TextField).

    so, if n7 "7" + prevnum would display 7 in the text field. when i press the button again i would like it to say 77 and if i pressed it again 777 but i have it set for ALL numbers so i could press button 1 button 5 and button 9 and i would get 159. BUT i dont! if i press seven again, i get 7. if i wer to press 9, the textArea that was displaying 7, would switch to 9, not add 9 to 7 for 79.

    I NEED HELP! PLEASE!!!

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

    Default

    Hello and welcome to our forum.

    First off, take a deep breath and relax. You'll find a solution.

    Next off, you need to understand what JTextArea#setText(String text) does. It replaces anything in the JTextArea with the String passed into the method, the text parameter. So anything that was on the JTextArea previously is gone. All is not lost, though as JTextArea has other methods including the append(String text) method. You would do well to read the JTextArea API and the tutorial to find the methods available to it.

    Luck!

  3. #3
    Jcbconway is offline Senior Member
    Join Date
    Sep 2010
    Posts
    109
    Rep Power
    0

    Default

    Agreed, and i know about append. BUT, the number1 = (n7 + prevnum) then the TF.setText(number1) should replace that string and get like i said 77? or is there something major with that logic?

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

    Default

    Quote Originally Posted by Jcbconway View Post
    Agreed, and i know about append. BUT, the number1 = (n7 + prevnum) then the TF.setText(number1) should replace that string and get like i said 77? or is there something major with that logic?
    I don't know, since I don't see the rest of your logic, but I do wonder where you are setting prevnum.

    You should consider posting an SSCCE (Short, Self Contained, Correct (Compilable), Example, a condensed program similar to your current program in that it demonstrates the problem at hand, but is the smallest bit of code that still compiles and has no extra code that's not relevant to your problem. For more info on SSCCEs please look here:

    SSCCE

  5. #5
    Jcbconway is offline Senior Member
    Join Date
    Sep 2010
    Posts
    109
    Rep Power
    0

    Default

    Actually, i fixed it. the reason i was not useing append was because i needed it to be 1 string, but i relized i could just use the gettext meathoud (or whatever the name was) and then its all 1 string anyways. thank you.

Similar Threads

  1. Help needed with string manipulation
    By stringkilla in forum New To Java
    Replies: 47
    Last Post: 09-07-2010, 04:25 AM
  2. Limiting only one character in a string field
    By jojanny in forum New To Java
    Replies: 4
    Last Post: 08-23-2010, 04:01 AM
  3. Replies: 4
    Last Post: 01-18-2010, 12:13 AM
  4. How to Highlight a String in JTextArea
    By ramvaidhya in forum AWT / Swing
    Replies: 6
    Last Post: 12-31-2008, 08:23 AM
  5. delete string in JtextArea
    By cloudet in forum AWT / Swing
    Replies: 3
    Last Post: 05-05-2008, 11:06 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •