Hi everybody I am a bit of stack here. When I run the program and press the button submit it is supposed to change 4 pictures in every 2 seconds.However it is not redisplaying the images. If anyone can give me a hand it would be great. I am using eclipse and the program is compiling and running. Here is the code.
/** Here is the GUI of the program
* class name SlideShowGui.java
* @author Kiril Anastasov
* @date 07/03/2012
*/
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
public class SlideShowGui extends JPanel implements ActionListener, Runnable
{
JLabel name, comments, images;
JTextField namejtf, commentsjtf, captionjtf;
JButton submit;
ImageIcon pictures1, pictures2, pictures3, pictures4;
//ImageIcon []pictures2 = {galileo1.jpg};
SlideShowGui()
{
name = new JLabel("Name:");
this.add(name);
namejtf = new JTextField(15);
this.add(namejtf);
comments = new JLabel("Comments:");
this.add(comments);
commentsjtf = new JTextField(15);
this.add(commentsjtf);
submit = new JButton("Submit");
this.add(submit);
submit.addActionListener(this);
pictures1 = new ImageIcon("galileo1.jpg");
images = new JLabel(pictures1);
this.add(images);
pictures2 = new ImageIcon("galileo2.jpg");
this.add(images);
pictures3 = new ImageIcon("galileo3.jpg");
this.add(images);
pictures4 = new ImageIcon("galileo4.jpg");
this.add(images);
captionjtf = new JTextField(24);
this.add(captionjtf);
//pictures = new ImageIcon("galileo1.jpg");
// images.setIcon(pictures);
}
public void actionPerformed(ActionEvent ae)
{
Thread t = new Thread(this);
t.start();
if(ae.getSource() == submit)
{
int i = 0;
boolean go = true;
while(go)
{
i++;
System.out.println(i);
try
{
Thread.sleep(2000);
if(i == 1)
{
pictures1 = new ImageIcon("galileo1.jpg");
images.setIcon(pictures1);
System.out.println("picture 1 should be displayed here");
}
if(i == 2)
{
pictures2 = new ImageIcon("galileo2.jpg");
images.setIcon(pictures2);
System.out.println("picture 2 should be displayed here");
}
if(i == 3)
{
pictures3 = new ImageIcon("galileo3.jpg");
images.setIcon(pictures3);
System.out.println("picture 3 should be displayed here");
}
if(i == 4)
{
pictures4 = new ImageIcon("galileo4.jpg");
images.setIcon(pictures4);
System.out.println("picture 4 should be displayed here");
}
if(i == 4)
{
i = 0;
}
}
catch (InterruptedException ie)
{
System.out.println("thread exception");
}
}
}
}
public void run()
{
}
}
/**The driver class of the program. Here is the JFrame
* class name TestSlideShow.java
* @author Kiril Anastasov
* @date 07/03/2012
*/
import java.awt.*;
import javax.swing.*;
public class TestSlideShow
{
public static void main(String[] args)
{
JFrame application = new JFrame();
SlideShowGui panel = new SlideShowGui();
application.add(panel);
application.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
application.setSize(300,600);
application.setLocation(400,100);
application.setVisible(true);
}
}
The whole
if (i == ...
part can be simplified. Declare a static class member inSlideShowGUI
:and use it in the replacement of the
if
statements:You can simplify your code as Andreas_D mentioned.
Your current design will block the main thread as you call
Thread.sleep()
to it, this will freeze your application.If you would like to update the
Image
, you should implement the update code insiderun()
method.So if you detect the user press on submit
JButton
, create and start the newThread
for updating UI.Always use
javax.swing.Timer
never useThread.sleep(...)
in Swing atleast. Here try this code, but do replace thepath
to your images :