Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9235345
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T07:00:30+00:00 2026-06-18T07:00:30+00:00

I have been looking for a way to display images on a JPanel in

  • 0

I have been looking for a way to display images on a JPanel in a grid-like fashion, and came across this solution. The code works very well but when I attempted to modify it by using jpg images instead of drawing rectangles, I could’t get it to work. My modification is as follows:

/*
 * courtesy of https://stackoverflow.com/questions/7222988/java-drag-and-drop-images-  in-a-list
 */

import java.awt.*;
import java.awt.datatransfer.DataFlavor;
import java.awt.datatransfer.Transferable;
import java.awt.datatransfer.UnsupportedFlavorException;
import java.io.IOException;
import java.io.Serializable;
import javax.swing.*;
import javax.swing.border.Border;

public class ImagePreviewList {

public static void main(String[] args) throws Exception {
    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
    JFrame frame = new JFrame("Image panel");
    frame.setSize(800, 300);
    frame.setLocationByPlatform(true);
    JList imageList = createImageList();
    frame.getContentPane().add(new JScrollPane(imageList));
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setVisible(true);
}

private static JList createImageList() {

    JList imageList = new JList(createModel());
    imageList.setCellRenderer(new ImageCellRenderer());
    imageList.setLayoutOrientation(JList.HORIZONTAL_WRAP);
    imageList.setVisibleRowCount(-1);
    imageList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
    imageList.setFixedCellWidth(240);
    imageList.setFixedCellHeight(120);

    imageList.setDragEnabled(true);
    imageList.setDropMode(DropMode.INSERT);
    imageList.setTransferHandler(new ImageTransferHandler(imageList));

    return imageList;
}

private static DefaultListModel createModel() {
    DefaultListModel model = new DefaultListModel();
    model.addElement(new RTS_image("3-Way_Stop_sign"));
    model.addElement(new RTS_image("4-Way_Stop_sign"));
    model.addElement(new RTS_image("No_Entry_sign"));
    model.addElement(new RTS_image("One-Way_Roadway_sign-0"));
    model.addElement(new RTS_image("One-Way_Roadway_sign-1"));
    model.addElement(new RTS_image("One-Way_Roadway_sign-3"));
    model.addElement(new RTS_image("Pedestrian-Priority_sign"));
    model.addElement(new RTS_image("Stop#Go_sign"));
    model.addElement(new RTS_image("Stop_and_Yield_sign"));
    model.addElement(new RTS_image("Stop_sign-0"));
    model.addElement(new RTS_image("Stop_sign-1"));
    model.addElement(new RTS_image("Yield_at_Mini-Circle_sign"));
    model.addElement(new RTS_image("Yield_sign"));
    model.addElement(new RTS_image("Yield_to_Pedestrians_sign"));
    model.addElement(new RTS_image("Yield_to_oncoming_traffic_sign"));
    return model;
}

static class ImageTransferHandler extends TransferHandler {

    private static final DataFlavor DATA_FLAVOUR = new DataFlavor(RTS_image.class,  "Images");

    private final JList previewList;
    private boolean inDrag;

    ImageTransferHandler(JList previewList) {
        this.previewList = previewList;
    }

    public int getSourceActions(JComponent c) {
        return TransferHandler.MOVE;
    }

    protected Transferable createTransferable(JComponent c) {
        inDrag = true;
        return new Transferable() {
            public DataFlavor[] getTransferDataFlavors() {
                return new DataFlavor[] {DATA_FLAVOUR};
            }

            public boolean isDataFlavorSupported(DataFlavor flavor) {
                return flavor.equals(DATA_FLAVOUR);
            }

            public Object getTransferData(DataFlavor flavor) throws UnsupportedFlavorException, IOException {
                return previewList.getSelectedValue();
            }
        };
    }

    public boolean canImport(TransferSupport support) {
        if (!inDrag || !support.isDataFlavorSupported(DATA_FLAVOUR)) {
            return false;
        }

        JList.DropLocation dl = (JList.DropLocation)support.getDropLocation();
        if (dl.getIndex() == -1) {
            return false;
        } else {
            return true;
        }
    }

    public boolean importData(TransferSupport support) {
        if (!canImport(support)) {
            return false;
        }

        Transferable transferable = support.getTransferable();
        try {
            Object draggedImage = transferable.getTransferData(DATA_FLAVOUR);

            JList.DropLocation dl = (JList.DropLocation)support.getDropLocation();
            DefaultListModel model = (DefaultListModel)previewList.getModel();
            int dropIndex = dl.getIndex();
            if (model.indexOf(draggedImage) < dropIndex) {
                dropIndex--;
            }
            model.removeElement(draggedImage);
            model.add(dropIndex, draggedImage);
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

    protected void exportDone(JComponent source, Transferable data, int action) {
        super.exportDone(source, data, action);
        inDrag = false;
    }
}

static class ImageCellRenderer extends JPanel implements ListCellRenderer {

    DefaultListCellRenderer defaultListCellRenderer = new DefaultListCellRenderer();
    JLabel imageLabel = new JLabel();
    JLabel descriptionLabel = new JLabel();

    ImageCellRenderer() {
        setLayout(new BorderLayout());
        Border emptyBorder = BorderFactory.createEmptyBorder(5, 5, 5, 5);
        imageLabel.setBorder(emptyBorder);
        descriptionLabel.setBorder(emptyBorder);
        add(imageLabel, BorderLayout.CENTER);
        add(descriptionLabel, BorderLayout.SOUTH);
    }

    public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) {
        defaultListCellRenderer.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus);
        setBorder(defaultListCellRenderer.getBorder());
        setBackground(defaultListCellRenderer.getBackground());
        imageLabel.setIcon((Icon)value);
        descriptionLabel.setText("Description");
        return this;
    }
}

static class RTS_image implements Icon, Serializable {
    private String Name_of_RoadSign; 

    RTS_image(String name_of_roadsign) {
        this.Name_of_RoadSign = name_of_roadsign;
    }

    java.net.URL imgURL = com.umusebo.RTS.ImagePreviewList.class.getResource("images/01_Regulatory_Signs/01_Control_Signs/reg_cont_" + Name_of_RoadSign + ".jpg");

    Image img = Toolkit.getDefaultToolkit().createImage(imgURL);

    public void paintIcon(Component c, Graphics g, int x, int y) {
        g.drawImage(img, x, y, c);
    }

    public int getIconWidth() {
        return 114;
    }

    public int getIconHeight() {
        return 125;
    }

    public String getName_of_RoadSign() {
        return Name_of_RoadSign;
    }

    public boolean equals(Object o) {
        if (o == null || getClass() != o.getClass()) {
            return false;
        }
        return Name_of_RoadSign.equals(((RTS_image)o).Name_of_RoadSign);
    }

}

}

Upon execution I get the following errors (which repeats 15 times for the 15 images I’m trying to display):

Uncaught error fetching image:
java.lang.NullPointerException
at sun.awt.image.URLImageSource.getConnection(URLImageSource.java:115)
at sun.awt.image.URLImageSource.getDecoder(URLImageSource.java:125)
at sun.awt.image.InputStreamImageSource.doFetch(InputStreamImageSource.java:263)
at sun.awt.image.ImageFetcher.fetchloop(ImageFetcher.java:205)
at sun.awt.image.ImageFetcher.run(ImageFetcher.java:169)

Where could the problem be?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T07:00:31+00:00Added an answer on June 18, 2026 at 7:00 am

    You’ve declared a number of class level field that you are trying to initialise using the values from other variables that are null

    For example, you are declaring and intialising imgURL from the variable Name_of_RoadSign, which will be null.

    java.net.URL imgURL = com.umusebo.RTS.ImagePreviewList.class.getResource(
        "images/01_Regulatory_Signs/01_Control_Signs/reg_cont_"
        + Name_of_RoadSign + ".jpg");
    

    You have to remember that the classes fields are initialised BEFORE any of the classes constructors are executed.

    Try initialising these class variables within constructor instead

    RTS_image(String name_of_roadsign) {
        this.Name_of_RoadSign = name_of_roadsign;
        imgURL = com.umusebo.RTS.ImagePreviewList.class.getResource(
            "images/01_Regulatory_Signs/01_Control_Signs/reg_cont_"
            + Name_of_RoadSign + ".jpg");
        img = Toolkit.getDefaultToolkit().createImage(imgURL);
    }
    

    ToolKit#createImage tends to silently ignore errors and will return a none null value even if the image can’t be loaded. You are much better of using ImageIO instead. Apart from the fact that it supports a greater range of image formats, it will also throw more meaningful exceptions, making it easier to diagnose problems.

    For example…

    img = ImageIO.read(imgURL);
    

    Don’t forget, this will throw an IOException so you either need to catch it or (better still) pass it back to the caller.

    I would also encourage you to familiarise yourself with the Code Conventions for the Java Programming Language. You won’t make any friends or influence people by ignoring them. 😉

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've been struggling looking for an understandable way to do this. I have four
I have been looking around the internet for a way to display specific content
I have been looking for a solution to this issue for a few days
This might have been asked before but I'm really looking for a simple way
I have been looking for a way to remove an attachment from Jira using
I have been looking for a way to get rid of the nasty black
I have been looking for a way to allow users to manually override geolocation
I have been looking at the Thinktecture.IdentityModel.40 library as a way of handling the
Hey i have been looking around and i cant find a way to get
I have been looking around and would like to know what everyone uses to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.