javaimageswingjframe

How to set an image filename


I made a Java program with Swing libraries that displays text and an image. Both the text string and the image filename are set via a set method, but while the text is displayed correctly, the image is not.

If I set the filename directly in the constructor then it works, but it is not what I want to do.

I cannot find a way to pass the filename to display the image.

The intention is to periodically pass an array of strings and a set of image filenames, to have a GUI with text and image changing over time.

Here is the code simplified:

import javax.swing.*;
import java.net.URL;

class Swing extends JFrame {
    
    private JLabel label = new JLabel();
    private String imageFilename = new String();
    
    Swing()
    {
        super("Swing Test Application"); 
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setSize(1000, 700);
        setLayout(null);
        setLocationRelativeTo(null);
            
        label.setBounds(10, 0, 220, 100); 
        add(label);
        
        //String imageFilename = "images\\image-1.jpg";     
        URL imageURL = getClass().getResource(imageFilename); 
        if (imageURL != null) {
            ImageIcon imageIcon = new ImageIcon(imageURL);
            JLabel imageLabel = new JLabel(imageIcon);
            imageLabel.setBounds(10, 200, 420, 420);
            add(imageLabel); // Aggiungi l'etichetta al frame
        } else {
            System.err.println("Image " + imageFilename + " not found.");
        }       
                    
        setVisible(true);
    }
    
    void SetText(String strLabel, String imageFilename) 
    {
        label.setText(strLabel);        
        this.imageFilename = imageFilename;
        
        return;
    }
        
    public static void main(String arg[]) 
    { 
        Swing swing = new Swing();      
        swing.SetText("This is string 1", "images\\image-1.jpg"); 
    }
}

Solution

  • In the same way that you declared a JLabel field for the text label, you can declare another JLabel field for the image label:

    private JLabel label = new JLabel();
    private JLabel image = new JLabel(); // <-------
    
    Swing()
    {
        super("Swing Test Application");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setSize(1000, 700);
        setLayout(null);
        setLocationRelativeTo(null);
    
        label.setBounds(10, 0, 220, 100);
        add(label);
        image.setBounds(10, 200, 420, 420);
        add(image);
        setVisible(true);
    }
    

    In SetText, call setIcon on the image label:

    void SetText(String strLabel, String imageFilename)
    {
        label.setText(strLabel);
        URL imageURL = getClass().getResource(imageFilename);
        if (imageURL != null) {
            ImageIcon imageIcon = new ImageIcon(imageURL);
            image.setIcon(imageIcon);
        } else {
            System.err.println("Image " + imageFilename + " not found.");
        }
    }