Results 1 to 4 of 4
Like Tree1Likes
  • 1 Post By Fubarable

Thread: Refresh JLabel

  1. #1
    Josep_16's Avatar
    Josep_16 is offline Member
    Join Date
    Mar 2011
    Posts
    40
    Rep Power
    0

    Exclamation Refresh JLabel

    Hello. I have some JLabels which contain some variables inside their Strings. The JLabels are components of a Container, which is also contained, which container is also contained, etc... Whatever, as the JLabels are added in the init method, there their variables equal to their initiation number, which is 0. As the Applet goes on, the variables, of course, change their state (value); and, as the JLabels were added in init(), ther variables wroten in the JLabels' Strings don't change their value.
    How can I "refresh" the value of the variable, or what's the same, something like "repaint" the JLabel, so it takes the actual value of the variable.

    A example of what I have would be like this:

    Java Code:
    public class Hello extends JApplet{
    
    int x;
    
    public void init(){
    x=0;
    JLabel label = new JLabel("x = "+x);
    add(label);
    }
    public void actionPerformed(ActionEvent evt){
    x ++;
    }
    }
    In this code, the JLabel will ever display x = 0, even if the action is performed.
    Hope you can help me. Thanks for support.

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,266
    Rep Power
    25

    Default Re: Refresh JLabel

    Look at the methods for the label. There are some that will change/set what the label shows.

    JLabel will ever display x = 0, even if the action is performed.
    The value of the variable x was copied into the text of the label. There is no reference/pointer to the x variable that will follow its changes. This is true for most variables in java. There are some objects such as models for JTables that will cause the display to update when the model's data is changed, but that takes lots of code to make happen. A primitive like an int does not provide that service.

  3. #3
    PhQ's Avatar
    PhQ
    PhQ is offline Senior Member
    Join Date
    Mar 2010
    Location
    Lithuania
    Posts
    358
    Rep Power
    5

    Default Re: Refresh JLabel

    You can try
    label.paintImmediately(label.getVisibleRect());
    to repaint the label.

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: Refresh JLabel

    Quote Originally Posted by PhQ View Post
    You can try
    label.paintImmediately(label.getVisibleRect());
    to repaint the label.
    This will not help the original poster, is not even close to solving his original problem, and will only serve to confuse him.
    DarrylBurke likes this.

Similar Threads

  1. refresh help
    By silverspoon34 in forum New To Java
    Replies: 6
    Last Post: 03-29-2011, 02:03 AM
  2. Cannot refresh a JLabel on a JFrame
    By carnotan in forum AWT / Swing
    Replies: 11
    Last Post: 03-25-2011, 03:02 PM
  3. Adding a JLabel to a JPanel - jlabel not showing
    By Bongeh in forum New To Java
    Replies: 17
    Last Post: 04-06-2010, 11:02 PM
  4. How to refresh my JInternalFrame upon CUD
    By chyrl in forum AWT / Swing
    Replies: 2
    Last Post: 04-01-2010, 04:22 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •