File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Swing / AWT / SWT and the fly likes Trying to repaint() a JLabel Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Java Interview Guide this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Trying to repaint() a JLabel" Watch "Trying to repaint() a JLabel" New topic

Trying to repaint() a JLabel

tom davies
Ranch Hand

Joined: Apr 27, 2012
Posts: 168
I am creating a small dice betting game with a GUI.
The users chooses which variation of the game they want to play and how much to bet, then press throw.
I have a JPanel which contains a JLabel, i want this JLabel to refresh with the users current balance when the throw button is pressed.
I have checked and it appears that the balance is being updated bu the JLabel is not being repainted.

Here is my actionListener

The JPanel that holds my balance JLabel.

When the button "roll" is pressed the method below is executed. As you can see i try to repaint the JLabel balance
Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632
unlike Excel, JLabel's don't have automatic recalculation - you need to set it manually

instead of
balance.setText("Balance = £" + playerBalance); //repaint() not needed - setText() handles that
tom davies
Ranch Hand

Joined: Apr 27, 2012
Posts: 168
Thank you! works great now.
I agree. Here's the link:
subject: Trying to repaint() a JLabel
jQuery in Action, 3rd edition