Reputation: 272
I am a bit of stuck. When I press the button submit is should redisplay another picture on the same position the JLabel images so if anyone have any ideas i would appreciate them 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
{
JLabel name, comments, images;
JTextField namejtf, commentsjtf, captionjtf;
JButton submit;
ImageIcon pictures;
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);
pictures = new ImageIcon("galileo1.jpg");
images = new JLabel(pictures);
this.add(images);
// pictures2 = new ImageIcon("galileo2.jpg");
// images2 = new JLabel(pictures2);
// this.add(images2);
captionjtf = new JTextField(24);
this.add(captionjtf);
public void actionPerformed(ActionEvent ae)
{
if(ae.getSource() == submit)
{
pictures = new ImageIcon("galileo2.jpg");
images = new JLabel(pictures);
System.out.println("test");
}
}
}
}
/**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);
}
}
Upvotes: 2
Views: 211
Reputation: 109815
in some of cases Icon/ImageIcon doesn't works (sucked from web or HDD)
pictures = new ImageIcon("galileo2.jpg");
pictures.getImage().flush();
images.setIcon(pictures);
Upvotes: 5
Reputation: 508
change this
if(ae.getSource() == submit)
{
pictures = new ImageIcon("galileo2.jpg");
images = new JLabel(pictures);
System.out.println("test");
}
to
if(ae.getSource() == submit)
{
pictures = new ImageIcon("galileo2.jpg");
images.setIcon(pictures);
System.out.println("test");
}
Upvotes: 5
Reputation: 5334
as far as I can tell, you're not adding the new image in your listener. I also think you need to call repaint, in order for the new image to be displayed:
public void actionPerformed(ActionEvent ae){
if(ae.getSource() == submit){
pictures = new ImageIcon("galileo2.jpg");
images = new JLabel(pictures);
this.add(images); //adding the new image to your JPanel
repaint(); //repaint in order to show the image
System.out.println("test");
}
}
Try this and it should work
Upvotes: 3