• 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

problem using button event handler to update text in a frame

 
Greenhorn
Posts: 19
Mac Eclipse IDE Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Marshal
Posts: 28193
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hint: JLabel does have a setText(String) method.
 
Michael Gordon
Greenhorn
Posts: 19
Mac Eclipse IDE Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Mac Eclipse IDE Linux
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Never trust an airline that limits their passengers to one carry on iguana. Put this tiny ad in your shoe:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic