Go Back   CodingForums.com > :: Server side development > Java and JSP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 04-13-2012, 03:46 AM   PM User | #1
Ericyue
New to the CF scene

 
Join Date: Apr 2012
Posts: 7
Thanks: 2
Thanked 0 Times in 0 Posts
Ericyue is an unknown quantity at this point
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 .
Ericyue is offline   Reply With Quote
Old 04-13-2012, 04:18 AM   PM User | #2
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,662
Thanks: 4
Thanked 2,452 Times in 2,421 Posts
Fou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to all
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.
Fou-Lu is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 10:28 AM.


Advertisement
Log in to turn off these ads.