Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
Thread: update jlabel image
04-13-2012, 04:46 AM #1
- Join Date
- Apr 2012
- Thanked 0 Times in 0 Posts
update jlabel image
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
public class Tester extends JWindow
public static void main(Stringargs)
int i = 1;
ImageIcon ii=new ImageIcon(i + ".jpeg");
JLabel label=new JLabel(ii);
JFrame frame=new JFrame("Show Image");
label.setIcon(new ImageIcon( i + "jpeg"));
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 .
04-13-2012, 05:18 AM #2
- Join Date
- Sep 2002
- Saskatoon, Saskatchewan
- 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.