New instance of JLabel being created when button clicked.
Post by:larry larrysteavens
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
Post by:Knute Snortum
Welcome to the Ranch!
Why don't you post the code where you think it should update the JLabel (and UseCodeTags).
Post by:Alex Kam
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.
Both myButton and myLabel variables must be marked as final.
Post by:Darryl Burke
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.
This thread has been viewed 512 times.
All times above are in ranch (not your local) time.
The current ranch time is Sep 22, 2018 10:56:27.