• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

JTextArea page 415 of Head First Java

 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Okay, well this is my first post here. I've been going through Head First Java and I've gotten up to chapter 13.

On page 415 there is a JTextArea example that looks like this:



I've played around with the code a bit just for my own experimentation and I was trying to get the code to make it so that after you had clicked the button so many times it would change the output.

Like if you pressed it a second time it would say 'button clicked twice'

and then again after it was pressed a third time it would say "button clicked thrice'

My instincts tell me that I would need an if/else statement but I can't get anything to work. The answer is probably more simple than it looks, but how would this be accomplished?
 
Ranch Hand
Posts: 479
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Put an instance variable in the text area class -- an integer that tracks how many times the button has been pushed.

In actionPerformed, test this integer to determine what to put in the text area.

Keep in mind that whatever you put in that area will stay there until the text area is destroyed or is cleared.

I leave the details to your inquring mind, on the assumption that, by page 4xx of Head First java, you know how to do those things.

rc

p.s. your actionPerformed also needs to update the instance variable...
 
J.E. McCracken
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Nope still can't get it to work. I'm really struggling with this book most of the exercises take me forever to reason out.

It would be helpful if I could see how it's done so I knew what I was doing wrong

 
Rancher
Posts: 1776
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
So show the code you modified on Ralph's guidance...
 
J.E. McCracken
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You'll probably laugh at me I wasn't able to get much. I had more but I removed some of it out of frustration :/
 
John Jai
Rancher
Posts: 1776
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
There was a compilation error - you have declared the firsttime boolean variable inside the go() method but tried to use that in the actionPerformed() method. When you declare variables inside a method, it is local to the particular method and its scope dies when you come out of the method. I have just changed the firsttime variable to an instance variable.

Reflecting on Ralph's idea I have added a instance variable named clickCounter in the below code. Think where you will increment it and show the value to user accordingly.




And nobody will laugh over your ignorance... Because we are all here to learn and read below forum quote

Howdy! This is your friendly forum for beginning Java.


 
J.E. McCracken
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you thank you thank you! This has been driving me crazy! I was closer than I thought.

One last question. How would I get it to display a different message after the third click? I tried adding another if statement & another boolean variable but that just added another line of text with the first button click.

 
John Jai
Rancher
Posts: 1776
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You don't need to create new boolean variables for handling conditions. Just check on the counter variable you already have in place. You can remove the firsttime variable itself.



And instead of the append() method of TextArea, try with the setText() method to clear previously shown text.
 
J.E. McCracken
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you again! That should be all.
 
John Jai
Rancher
Posts: 1776
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You are welcome...
 
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Your code doesn't need "JButton button ;" as a field/instance variable. Only "JTextArea text; " is needed since it's needed for method actionPerformed().
 
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to the Ranch Ivan Turner.
 
I think I'll just lie down here for a second. And ponder this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic