• Post Reply Bookmark Topic Watch Topic
  • New Topic

problem using button event handler to update text in a frame  RSS feed

 
Michael Gordon
Greenhorn
Posts: 19
Eclipse IDE Linux Mac
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,

I have created a FortuneTeller Class which creates a frame and button. When the button is pressed a random number is generated which is then used to select a string from an array full of possible fortunes. At the minute, I can only display the fortune in my eclipse console using System.out.println(output); However, I would like to make the fortune appear on the frame alongside the button being pressed. My plan is for each time the button is pressed, the current fortune being displayed is erased and a new one is presented in its place.

The problem I am having is that I can't figure out how to display anything on the frame unless it is a fixed string using JLabel textLabel = new JLabel("text for fortune to go here",SwingConstants.CENTER); I'd like to use the output from the ButtonHandler Class, which would be somehow using the 'output' string variable.

Any advice would be much appreciated at this point as I feel like I'm hitting a brick wall. I know my output variable is of type String, and that output can't be used outwith the ButtonHandler class, but I can't figure out how to transfer that data to a more accessible variable.

I've attached my code, both classes for perusal if you have a few spare minutes.


Main FortuneTeller Class



RandomNumberSelector Class
 
Paul Clapham
Sheriff
Posts: 22841
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hint: JLabel does have a setText(String) method.
 
Michael Gordon
Greenhorn
Posts: 19
Eclipse IDE Linux Mac
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Paul Clapham wrote:Hint: JLabel does have a setText(String) method.


Hi Paul,

But I still don't know how to use my 'output' variable from my ButtonHandler Class. I'm not sure how to get the output from ButtonHandler Class to be used as the String argument for that method.




Michael
 
Michael Gordon
Greenhorn
Posts: 19
Eclipse IDE Linux Mac
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Oh god,

That has to be the most stupid thing I've said ever. haha. Was a bit tired last night, I got it fixed up and working all shiny now.

Think what I was doing wrong was that I tried to assign the output variable to the frame outside the ButtonHandler Class.

Thanks for the hint. It really was just staring me right in the face. In future I must remember that Javadocs are my friend.



Michael
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!