Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 6 of 6

Thread: Update JLabel with new ImageIcon

  1. #1
    Member
    Join Date
    Apr 2013
    Posts
    43
    Thanks
    9
    Thanked 0 Times in 0 Posts

    Default Update JLabel with new ImageIcon

    Hello!

    I have a JFrame containing a JLabel, JButton and a JScrollPane like:

    JButton b = new JButton();
    JLabel l = new JLabel();
    JScrollPane s = new JScrollPane(l);

    From the constructor a method, getImage, is called. And the method look like this:

    private void getImage() throws MalformedURLException {
     
     
            ImageIcon img = new ImageIcon(new URL("www.blablabla.com"));
     
            l.setIcon(img);
     
            repaint();
     
        }


    The url-address is generating a random picture every time you visit the page.
    The method above is also called when I'm clicking the JButton. The problem is that the image in the JLabel isn't updating. It only works when I'm starting the program.


    Hank


  2. #2
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,520
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Update JLabel with new ImageIcon

    Show the logic that periodically loads a new image and changes the JLabel's image.

    Give your variables better names. Single-letter variable names are almost never appropriate except for some limited index/counters uses.

  3. #3
    Member
    Join Date
    Apr 2013
    Posts
    43
    Thanks
    9
    Thanked 0 Times in 0 Posts

    Default Re: Update JLabel with new ImageIcon

    From within the constructor the getImage-method is called and it works perfectly. When I'm clicking at the JButton the actionPerformed-method is called, and within it the getImage-method is also called. It works, but no new picture is generated.

    Hank

  4. #4
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,520
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Update JLabel with new ImageIcon

    Then show the button's actionPerformed() method. You're also calling repaint(), but it's not clear on what component the repaint() is being called, so clarify that. Also, show that component's paintComponent() method.

  5. The Following User Says Thank You to GregBrannon For This Useful Post:

    iHank (February 6th, 2014)

  6. #5
    Member
    Join Date
    Apr 2013
    Posts
    43
    Thanks
    9
    Thanked 0 Times in 0 Posts

    Default Re: Update JLabel with new ImageIcon

    Thanks for the help, man. It works now as I used ImageIO.read(InputStream) instead of url directly.

  7. #6
    Member andbin's Avatar
    Join Date
    Dec 2013
    Location
    Italy
    Posts
    443
    Thanks
    4
    Thanked 122 Times in 114 Posts

    Default Re: Update JLabel with new ImageIcon

    Quote Originally Posted by iHank View Post
    Thanks for the help, man. It works now as I used ImageIO.read(InputStream) instead of url directly.
    The problem was not the URL by itself. The problem with ImageIcon is that it uses the machinery in java.awt.Toolkit to load the image. And Toolkit uses a "caching" strategy: for the same url/filename you get the same Image object. The second time you get the same Image object, even if the file content/url stream is physically changed. By the way, this can be solved .... with the flush() of Image.

    Keep also in mind that ImageIO loads the image synchronously, while Toolkit/ImageIcon loads asynchronously. A synchronous loading can have a bad impact on your app (especially if you do this in the context of the EDT, Event Dispatch Thread).
    Andrea, www.andbin.netSCJP 5 (91%) – SCWCD 5 (94%)

    Useful links for Java beginnersMy new project Java Examples on Google Code

Similar Threads

  1. suggestions required on Jlabel(ImageIcon) and Jbutton for mouselistener
    By syedfahadjalali in forum What's Wrong With My Code?
    Replies: 3
    Last Post: November 15th, 2013, 02:12 AM
  2. How to update a JLabel in a basic Graphics exercise
    By hairLess in forum What's Wrong With My Code?
    Replies: 9
    Last Post: February 14th, 2013, 02:40 PM
  3. Loop through JLabel and change JLabel
    By JoeBrown in forum What's Wrong With My Code?
    Replies: 2
    Last Post: April 11th, 2012, 12:52 PM
  4. Auto Update JLabel on JButton Press?
    By Java Programmer in forum What's Wrong With My Code?
    Replies: 2
    Last Post: March 11th, 2012, 06:55 PM
  5. Cannot update Jlabel in JApplet
    By rin in forum Java Applets
    Replies: 2
    Last Post: April 17th, 2010, 08:21 AM