• 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

New instance of JLabel being created when button clicked.

 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 7125
184
Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Netbeans IDE Opera Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
reply
    Bookmark Topic Watch Topic
  • New Topic