Note that there are some explanatory texts on larger screens.

plurals
  1. POUpdating an image contained in a JLabel - problems
    primarykey
    data
    text
    <p>The part of the application that I am currently having trouble getting to work is being able to scroll through and display a list of images, one at a time. I'm getting a directory from the user, spooling through all of the files in that directory, and then loading an array of just the jpegs and pngs. Next, I want to update a JLabel with the first image, and provide previous and next buttons to scroll through and display each image in turn. When I try to display the second image, it doesn't get updated... Here's what I've got so far:</p> <pre class="lang-java prettyprint-override"><code>public class CreateGallery { private JLabel swingImage; </code></pre> <p>The method that I'm using to update the image:</p> <pre class="lang-java prettyprint-override"><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)); swingImage = tempImage; } </code></pre> <p>Then in my createAndShowGUI method that puts the swingImage on...</p> <pre class="lang-java prettyprint-override"><code>private void createAndShowGUI() { //Create and set up the window. final JFrame frame = new JFrame(); // Miscellaneous code in here - removed for brevity // Create the Image Thumbnail swingImage and start up with a default image swingImage = new JLabel(); String rootPath = new java.io.File("").getAbsolutePath(); updateImage(rootPath + "/images/default.jpg"); // Miscellaneous code in here - removed for brevity rightPane.add(swingImage, BorderLayout.PAGE_START); frame.add(rightPane, BorderLayout.LINE_END); </code></pre> <pre class="lang-java prettyprint-override"><code>public static void main(String[] args) { SwingUtilities.invokeLater(new Runnable() { public void run() { UIManager.put("swing.boldMetal", Boolean.FALSE); new CreateGalleryXML().createAndShowGUI(); } }); } </code></pre> <p>If you've gotten this far, the first image is my default.jpg, and once I get the directory and identify the first image in that directory, that's where it fails when I try to update the swingImage. Now, I've tried to swingImage.setVisible() and swingImage.revalidate() to try to force it to reload. I'm guessing it's my tempImage = new JLabel that's the root cause. But I'm not sure how to convert my BufferedImage or Image to a JLabel in order to just update swingImage.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    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