Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Instead of creating a <code>New Instance</code> of the <code>JLabel</code> for each <code>Image</code>, simply use <a href="http://docs.oracle.com/javase/7/docs/api/javax/swing/JLabel.html#setIcon(javax.swing.Icon)" rel="nofollow noreferrer">JLabel#setIcon(...)</a> method of the <code>JLabel</code> to change the image.</p> <p>A small sample program : </p> <pre><code>import java.awt.*; import java.awt.event.*; import javax.swing.*; public class SlideShow extends JPanel { private int i = 0; private Timer timer; private JLabel images = new JLabel(); private Icon[] icons = {UIManager.getIcon("OptionPane.informationIcon"), UIManager.getIcon("OptionPane.errorIcon"), UIManager.getIcon("OptionPane.warningIcon")}; private ImageIcon pictures1, pictures2, pictures3, pictures4; private ActionListener action = new ActionListener() { public void actionPerformed(ActionEvent ae) { i++; System.out.println(i); if(i == 1) { pictures1 = new ImageIcon("image/caIcon.png"); images.setIcon(icons[i - 1]); System.out.println("picture 1 should be displayed here"); } if(i == 2) { pictures2 = new ImageIcon("image/Keyboard.png"); images.setIcon(icons[i - 1]); System.out.println("picture 2 should be displayed here"); } if(i == 3) { pictures3 = new ImageIcon("image/ukIcon.png"); images.setIcon(icons[i - 1]); System.out.println("picture 3 should be displayed here"); } if(i == 4) { pictures4 = new ImageIcon("image/Mouse.png"); images.setIcon(icons[0]); System.out.println("picture 4 should be displayed here"); } if(i == 5) { timer.stop(); System.exit(0); } revalidate(); repaint(); } }; public SlideShow() { JFrame frame = new JFrame("SLIDE SHOW"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setLocationByPlatform(true); frame.getContentPane().add(this); add(images); frame.setSize(300, 300); frame.setVisible(true); timer = new Timer(2000, action); timer.start(); } public static void main(String... args) { SwingUtilities.invokeLater(new Runnable() { public void run() { new SlideShow(); } }); } } </code></pre> <p>Since you doing it with ImageIO, here is a good example related to that <a href="https://stackoverflow.com/questions/9721421/show-image-by-click-jbutton/9722205#9722205">JLabel using ImageIO</a></p> <p>Information relating to your case, as to what is happening : </p> <p>Inside your <code>createAndShowGUI()</code> method you initializing your <code>JLabel</code> (swingImage), and you added that to your <code>JPanel</code> by virtue of which indirectly to the <code>JFrame</code>. </p> <p>But now inside your <code>updateImage()</code> method, you are initializing a new <code>JLabel</code>, now it resides in some another memory location, by writing <code>tempImage = new JLabel(new ImageIcon(scaledImage));</code> and after this you pointing your <code>swingImage(JLabel)</code> to point to this newly created <code>JLabel</code>, but this newly created <code>JLabel</code> was never added to the <code>JPanel</code>, at any point. Hence it will not be visible, even if you try <code>revalidate()/repaint()/setVisible(...)</code>. Hence either you change the code for your <code>updateImage(...)</code> method to this : </p> <pre><code>protected void updateImage(String name) { BufferedImage image = null; Image scaledImage = null; JLabel tempImage; try { image = ImageIO.read(new File(name)); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } // getScaledImage returns an Image that's been resized // proportionally to my thumbnail constraints scaledImage = getScaledImage(image, THUMB_SIZE_X, THUMB_SIZE_Y); tempImage = new JLabel(new ImageIcon(scaledImage)); rightPane.remove(swingImage); swingImage = tempImage; rightPane.add(swingImage, BorderLayout.PAGE_START); rightPane.revalidate(); rightPane.repaint(); // required sometimes } </code></pre> <p>Or else use <code>JLabel.setIcon(...)</code> as mentioned earlier :-)</p> <p><strong><em>UPDATED THE ANSWER</em></strong></p> <p>Here see how a <code>New JLabel</code> is placed at the position of the old one, </p> <pre><code>import java.awt.*; import java.awt.event.*; import javax.swing.*; public class SlideShow extends JPanel { private int i = 0; private Timer timer; private JLabel images = new JLabel(); private Icon[] icons = {UIManager.getIcon("OptionPane.informationIcon"), UIManager.getIcon("OptionPane.errorIcon"), UIManager.getIcon("OptionPane.warningIcon")}; private ActionListener action = new ActionListener() { public void actionPerformed(ActionEvent ae) { i++; System.out.println(i); if(i == 4) { timer.stop(); System.exit(0); } remove(images); JLabel temp = new JLabel(icons[i - 1]); images = temp; add(images); revalidate(); repaint(); } }; private void createAndDisplayGUI() { JFrame frame = new JFrame("SLIDE SHOW"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setLocationByPlatform(true); this.setLayout(new FlowLayout(FlowLayout.LEFT)); add(images); frame.getContentPane().add(this, BorderLayout.CENTER); frame.setSize(300, 300); frame.setVisible(true); timer = new Timer(2000, action); timer.start(); } public static void main(String... args) { SwingUtilities.invokeLater(new Runnable() { public void run() { new SlideShow().createAndDisplayGUI(); } }); } } </code></pre> <p>And for your Question : <strong>Of the two options that I have tried, is one better than the other?</strong></p> <p><code>setIcon(...)</code> has an edge over the other way, in the sense, you doesn't have to bother about revalidate()/repaint() thingy after adding/remove <code>JLabel</code>. Moreover, you don't need to remember the placement of your <code>JLabel</code> everytime, you add it. It remains at it's position, and you simply call the one method to change the image, with no strings attached and the work is done, without any headaches.</p> <p>And for Question 2 : I had a bit of doubt, as to what is <code>Array of Records</code> ?</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload