JLabel Icon Image Update

I display an image in a JFrame using JLabel and setting its icon.

It works for the first time, but whenever I go to change the image, it remains what I installed for the first time, so I tried this and all the same result.

contentPane.remove(lblPlaceholder); lblPlaceholder = null; lblPlaceholder = new JLabel(""); lblPlaceholder.setBounds(10, 322, 125, 32); contentPane.add(lblPlaceholder); lblPlaceholder.setIcon(new ImageIcon("tempimage.png")); 

How can I change it? I also tried redrawing the JFrame without any results.

+4
source share
3 answers

Works great for me. I think there is something else in the code that you are not using. A SSCCE will help clarify other issues.

Some suggestions based on what you have provided ...

  • Avoid null layouts (it looks like you can use one)
  • Avoid setBounds

enter image description hereenter image description here

 import java.awt.BorderLayout; import java.awt.EventQueue; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.image.BufferedImage; import java.io.File; import java.io.IOException; import java.util.ArrayList; import java.util.List; import javax.imageio.ImageIO; import javax.swing.ImageIcon; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.UIManager; import javax.swing.UnsupportedLookAndFeelException; public class ShowLabelImage { public static void main(String[] args) { new ShowLabelImage(); } private JLabel label; private List<BufferedImage> images; private int currentPic = 0; public ShowLabelImage() { EventQueue.invokeLater(new Runnable() { @Override public void run() { try { UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) { } images = new ArrayList<>(2); try { images.add(ImageIO.read(new File("path/to/pic1"))); images.add(ImageIO.read(new File("path/to/pic2"))); } catch (IOException exp) { exp.printStackTrace(); } label = new JLabel(); label.setHorizontalAlignment(JLabel.CENTER); label.setVerticalAlignment(JLabel.CENTER); JButton switchPic = new JButton("Switch"); switchPic.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { currentPic++; if (currentPic >= images.size()) { currentPic = 0; } label.setIcon(new ImageIcon(images.get(currentPic))); } }); JFrame frame = new JFrame("Testing"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setLayout(new BorderLayout()); frame.add(label); frame.add(switchPic, BorderLayout.SOUTH); switchPic.doClick(); frame.pack(); frame.setLocationRelativeTo(null); frame.setVisible(true); } }); } } 
+6
source

There is no need to create a new shortcut, and this probably gets in the way.

Edit:

  contentPane.remove(lblPlaceholder); lblPlaceholder = null; lblPlaceholder = new JLabel(""); lblPlaceholder.setBounds(10, 322, 125, 32); contentPane.add(lblPlaceholder); lblPlaceholder.setIcon(new ImageIcon("tempimage.png")); 

To:

  lblPlaceholder.setIcon(new ImageIcon("tempimage.png")); 

See also this working example .

5JXpC.gif

Additional tips

  • The Java GUI may have to work on multiple platforms, at different screen resolutions and using different PLAFs. Thus, they do not contribute to the precise placement of the components. To organize components for a robust graphical interface, use layout managers or combinations of them instead, as well as layout and borders for free space. EG. The graphical interface above uses a GridBagLayout to center the image within the JScrollPane .
  • ASCII graphic of the graphical interface is provided for reference with mock-ups, since it should be displayed in the smallest size and (when resizing) with an additional width / height.
  • Publish SSCCE to better help.
+6
source

If you look at the ImageIcon constructor, you will see that it loads the icon using this method: image = Toolkit.getDefaultToolkit().getImage(location); The documentation for this method says:

  * Returns an image which gets pixel data from the specified URL. * The pixel data referenced by the specified URL must be in one * of the following formats: GIF, JPEG or PNG. * The underlying toolkit attempts to resolve multiple requests * with the same URL to the same returned Image. 

To update ImageIcon every time you load it from the same file name, you must add something random to the end of the URL without changing the actual file name. Something like this should work:

  try { URL url = new URL(file.toURI().toString() + "?" + System.currentTimeMillis()); jLabel1.setIcon(new ImageIcon(url)); } catch (MalformedURLException ex) { ex.printStackTrace(); } 
0
source

Source: https://habr.com/ru/post/1489829/


All Articles