This week's book giveaway is in the Cloud/Virtualization forum.
We're giving away four copies of Learning OpenStack Networking: Build a solid foundation in virtual networking technologies for OpenStack-based clouds and have James Denton on-line!
See this thread for details.
Win a copy of Learning OpenStack Networking: Build a solid foundation in virtual networking technologies for OpenStack-based clouds this week in the Cloud/Virtualization forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Liutauras Vilda
  • Campbell Ritchie
  • Tim Cooke
  • Bear Bibeault
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Knute Snortum
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Ganesh Patekar
  • Stephan van Hulst
  • Pete Letkeman
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Ron McLeod
  • Vijitha Kumara

New instance of JLabel being created when button clicked.  RSS feed

 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi there guys,

I'm creating a currency converter and the problem that's been bugging me is that every time I run the program, I think a new instance of JLabel is being created, when in fact I need the one that is already set to be updated. I have tried what seems like everything to fix this but I can't figure it out.

I click in the digits I like, using a keypad of JButtons which update the default Label of "0" to the digits I have clicked, but when I click the "Euro" button it is meant to update the JLabel with the converted currency.
I know that the currency is being converted successfully as I have print statements showing me, but I can not get it to update the JLabel in my GUI class.

I hope you guys can be of some help.

thanks in advance

 
Sheriff
Posts: 5112
138
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch!

Why don't you post the code where you think it should update the JLabel (and UseCodeTags).
 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It is difficult to say not seeing the code.

In case I understood correctly, an approach here is to add a listener to the JButton class instance. In the listener you should use getText() method to get String from the button. After that you should use the String in setText() to set up the String in the JLabel instance.

E.g.



Both myButton and myLabel variables must be marked as final.
 
Bartender
Posts: 5167
11
Java Netbeans IDE Opera
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Alex Kam wrote:Both myButton and myLabel variables must be marked as final.


... only if they are local variables, and you aren't using the latest version of Java viz. 8.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!