This week's book giveaway is in the HTML/CSS/JavaScript forum.
We're giving away four copies of Practical SVG and have Chris Coyier on-line!
See this thread for details.
Win a copy of Practical SVG this week in the HTML/CSS/JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

New instance of JLabel being created when button clicked.

 
larry larrysteavens
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

 
Knute Snortum
Sheriff
Posts: 3282
84
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).
 
Alex Kam
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.
 
Darryl Burke
Bartender
Posts: 5155
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.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!