Results 1 to 2 of 2

Thread: Accessing file

  1. #1
    Doctor Cactus is offline Member
    Join Date
    Oct 2008
    Rep Power

    Default Accessing file

    Hi, I am writing a java application. I have my menu created, but the problem is displaying an image with it. I can do this if I give the full path to the image, but I have the image stored in a subfolder of the current directory called "Images". How can i display the image without giving the full path to it? Could someone give a code example please.

    Help is much appreciated.

  2. #2
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Rep Power


    This is the hardest part of learning to use images in java.
    Java Code:
    import java.awt.GridLayout;
    import java.awt.image.BufferedImage;
    import javax.imageio.ImageIO;
    import javax.swing.*;
    public class Test {
        public static void main(String[] args) throws IOException {
            String path = "images/hawk.jpg";
            BufferedImage image = File(path));
   url = Test.class.getResource(path);
            System.out.println("url = " + url);
            BufferedImage image2 =;;
            JPanel panel = new JPanel(new GridLayout(1,0,10,0));
            panel.add(new JLabel(new ImageIcon(image)));
            panel.add(new JLabel(new ImageIcon(image2)));
            JOptionPane.showMessageDialog(null, panel, "images",
    See the section Loading Images Using getResource in the Using Icons page.
    It has links to sections in the javadoc guides at the end of the section.

Similar Threads

  1. Accessing Data from a .txt file
    By Oasis13 in forum New To Java
    Replies: 5
    Last Post: 02-01-2008, 12:16 AM
  2. Randomly accessing a text file
    By bugger in forum New To Java
    Replies: 1
    Last Post: 12-07-2007, 06:20 PM
  3. Accessing a file randomly
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 11-10-2007, 08:15 PM
  4. Accessing client file system
    By revathi17 in forum New To Java
    Replies: 1
    Last Post: 10-17-2007, 10:17 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts