Results 1 to 7 of 7
  1. #1
    jdg951 is offline Member
    Join Date
    Dec 2010
    Posts
    7
    Rep Power
    0

    Default Help with incrementing numbers in a GUI

    I have a GUI that I want to display a number (in a JTextArea) incremented by one and decreased by one when I click on the corresponding button. It will start from 0. So clicking the increase button twice will print 1 on the next line then 2 on the next line in the area. I already have the GUI set up.

    My question is how I would go about having my program read the latest line and increment that by one.

    I currently have
    Java Code:
    {
    	int value = (Integer.parseInt(jtaArea.getText()) + 1);		
    	jtaArea.append("\n" + Integer.toString(value));
    }
    but that one works from 0 to 1, clicking the button returns an error, I believe because it is trying to read both of the lines. (Note: I need to keep the numbers in the field and cannot erase them when I increment)

    Thanks in advance for any help.

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

    Default

    Do you have to read in the value? Can't you just hold the number as an int field of the class and not worry about reading it in from the text component?

  3. #3
    jdg951 is offline Member
    Join Date
    Dec 2010
    Posts
    7
    Rep Power
    0

    Default

    not entirely sure what you are talking about, but is what I want possible?

  4. #4
    JavaHater is offline Senior Member
    Join Date
    Dec 2010
    Posts
    165
    Rep Power
    5

    Default

    i suggest you show more examples of your code.

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

    Default

    Yes, you could read in the text held by the JTextArea, split the String on the line feed String and get the last token, but why? I would recommend not doing this, that is trying to read the last line of the JTextArea because it is completely unnecessary and thus an unnecessary complexity. Instead simply give your program an int variable, say called numberValue, and when the increase button is pressed, increase the value of numberValue by one and display it in the JTextArea. Conversely when the decrease button is pressed, decrease numberValue by one, and again display it. Again, unless your assignment instructions explicitly tell you otherwise, there is simply no need to read the values held by the JTextArea.

  6. #6
    jdg951 is offline Member
    Join Date
    Dec 2010
    Posts
    7
    Rep Power
    0

    Default

    ok I think I'm allowed to do that, thanks.

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

Similar Threads

  1. incrementing array elements
    By porchrat in forum New To Java
    Replies: 2
    Last Post: 10-06-2010, 03:37 PM
  2. incrementing elements in an array
    By JavaNubb in forum New To Java
    Replies: 7
    Last Post: 08-20-2010, 04:15 AM
  3. Incrementing and loops
    By Rubidoux in forum New To Java
    Replies: 1
    Last Post: 04-24-2010, 03:04 PM
  4. Incrementing jbutton in for loop
    By RobertF in forum New To Java
    Replies: 3
    Last Post: 03-12-2009, 12:41 PM
  5. Replies: 1
    Last Post: 05-29-2008, 07:36 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
  •