Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    New to the CF scene
    Join Date
    Apr 2012
    Posts
    7
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Exclamation update jlabel image

    hi ,

    i am doing a program which will display the image and update after x seconds. i have set the image name to be 1.jpeg , 2.jpeg .. till 100.jpeg. below is the code i have done so far

    import java.util.*;
    import java.awt.*;
    import java.io.*;
    import javax.swing.*;



    public class Tester extends JWindow
    {
    public static void main(String[]args)
    {
    int i = 1;
    ImageIcon ii=new ImageIcon(i + ".jpeg");
    JLabel label=new JLabel(ii);
    JFrame frame=new JFrame("Show Image");
    frame.add(label);

    while(i<6)
    {
    i++;
    label.setIcon(new ImageIcon( i + "jpeg"));
    }

    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setSize(400,400);
    frame.setVisible(true);


    }
    }

    it can compile without error but when i run it, it only show the frame with empty gray background. can anyone tell me how to settle this problem.

    thanks in advance for helping me .. thanks a lot .

  • #2
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    Label hasn't been specified a size, so it's likely trying to pack it as small as it can. Try setting the size to that of the maximum image. Make sure you repaint it as well.

    This isn't timed either. The while loop is irrelevant at this step since visibility hasn't been provided to the frame. You'll need to invoke the change in the label in a separate thread of its own, or call a loop after the frame is visible. To time it, you will want to use Thread.sleep or use a Timer to invoke a new Timertask. The while loop will also only update the label once, then complete before moving into the render of the JFrame itself.

    Just looking at it, this to me appears to toss runtime exceptions. ImageIcon will likely fail to find a file call Xjpeg, at minimum that should be looking for X.jpeg as an extension.


  •  

    Posting Permissions

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