File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes JTextArea page 415 of Head First Java Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "JTextArea page 415 of Head First Java" Watch "JTextArea page 415 of Head First Java" New topic
Author

JTextArea page 415 of Head First Java

J.E. McCracken
Greenhorn

Joined: Nov 15, 2011
Posts: 5
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?
Ralph Cook
Ranch Hand

Joined: May 29, 2005
Posts: 479
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

Joined: Nov 15, 2011
Posts: 5
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

John Jai
Bartender

Joined: May 31, 2011
Posts: 1776
So show the code you modified on Ralph's guidance...
J.E. McCracken
Greenhorn

Joined: Nov 15, 2011
Posts: 5
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
Bartender

Joined: May 31, 2011
Posts: 1776
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

Joined: Nov 15, 2011
Posts: 5
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
Bartender

Joined: May 31, 2011
Posts: 1776
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

Joined: Nov 15, 2011
Posts: 5
Thank you again! That should be all.
John Jai
Bartender

Joined: May 31, 2011
Posts: 1776
You are welcome...
Ivan Turner
Ranch Hand

Joined: Feb 27, 2012
Posts: 37
Your code doesn't need "JButton button ;" as a field/instance variable. Only "JTextArea text; " is needed since it's needed for method actionPerformed().
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 40034
    
  28
Welcome to the Ranch Ivan Turner.
 
 
subject: JTextArea page 415 of Head First Java