import javax.swing.*;
import java.awt.*;
public class sample
{
public static final int width=500;
public static final int height=500;

public static void main(String[] args)
{
JFrame mywindow=new JFrame();
myWindow.setSize(Width, height);
JLabel myLabel= new JLabel ("How do i insert picture in this window? I want to create a photo gallery on this window..can you help me generate the code?pls thanks!");
myWindow.getContentPane().add(myLabel);

myWindow.setVisible(true);
}
}

Dani AI

Generated

The sample in the first post has a few simple mistakes that will prevent it from compiling (case-sensitive names like mywindow vs myWindow and Width vs width) and it never calls setDefaultCloseOperation(...). As pointed out, putting an icon on a JLabel is the quickest way to show one image, but for a photo gallery you usually want thumbnails, a scrollable grid, and background loading so the UI does not freeze. linked to a tutorial — useful — but the gallery workflow below fills the gap between “show one icon” and a usable gallery.

A compact, practical approach: load images with ImageIO, create small thumbnails with Graphics2D (use rendering hints for quality), add each thumbnail as an ImageIcon on a JLabel, and place those labels into a JPanel with GridLayout inside a JScrollPane. Load full files off the Event Dispatch Thread (use SwingWorker) and only publish thumbnails back to Swing.

import javax.swing.*;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import javax.imageio.ImageIO;

public class PhotoGalleryPanel extends JPanel {
    public PhotoGalleryPanel(File dir, int thumbW, int thumbH) throws Exception {
        setLayout(new GridLayout(0, 4, 6, 6));
        File[] files = dir.listFiles(new java.io.FilenameFilter() {
            public boolean accept(File d, String name) {
                String l = name.toLowerCase();
                return l.endsWith(".jpg") || l.endsWith(".jpeg") || l.endsWith(".png") || l.endsWith(".gif");
            }
        });
        if (files == null) return;
        for (File f : files) {
            BufferedImage img = ImageIO.read(f);
            if (img == null) continue;
            BufferedImage thumb = new BufferedImage(thumbW, thumbH, BufferedImage.TYPE_INT_ARGB);
            Graphics2D g = thumb.createGraphics();
            g.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);
            g.drawImage(img, 0, 0, thumbW, thumbH, null);
            g.dispose();
            add(new JLabel(new ImageIcon(thumb)));
        }
    }
}

Practical tips and pitfalls:

  • Use SwingUtilities.invokeLater when creating your frame and SwingWorker for loading images to keep the UI responsive.
  • Prefer thumbnails (e.g., 120–200 px) and avoid keeping many full-resolution images in memory.
  • If images are inside your JAR, load them via getResource(...) and pass the URL to ImageIO.read(...).
  • If an image does not appear, check the working directory, file names (case on some OSes), and catch/log IO exceptions.
  • For a modern alternative with richer media support, consider JavaFX (OpenJFX) for new projects.

For API reference see the Swing icon guide (How to Use Icons) and Image I/O docs (javax.imageio.ImageIO).

Recommended Answers

All 3 Replies

Do you want to place an icon in your label ?
JLabel myLabel =new JLabel ("mylabel", new ImageIcon("images/pics.png"));

I already gave him a link to a tutorial that demonstrated this, but obviously he didn't feel compelled to read it (nor the instructions to use code tags).

Salam,
please inform me how 2 develop a web page in Macromedia Flash

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.