-1

I am creating a simple photo catalogue using JAVA and Netbeans. I have the basic Swing layout all OK and I can store and retrieve text/number etc from/to the database, delta records etc etc. This is all Fine.

I am really not sure how to go about storing the images and viewing them in the jFrame.

I want to store the images as a file path I think (as opposed to BLOB) as I have read this is better?

I am using getText and setText to display the database entries on the form at present and this is all fine. Like below:

String location = rs.getString("Location");

textLocation.setText(location);

I am using the JavaDB derby database connection.

I have searched for a tutorial but have not had much luck. There are tutees fro images etc but not really how to store/retrieve them from a database.

If anyone can point me in the right direction or to a good tutorial covering this that would be grand.

* UPDATE *

Thanks for the help. I thought I would just share what ended up working.

Set path to location of images folder:

private static final String PATH = "/images/";

Assign string

String image = rs.getString("ImageURL");

Add image to label

image_label.setIcon(new javax.swing.ImageIcon(getClass().getResource(PATH + image)));

Thanks again.

pelagos
  • 1,025
  • 3
  • 17
  • 27
  • possible duplicate of [How do i load images in Jframe java(eclipse)](http://stackoverflow.com/questions/18871150/how-do-i-load-images-in-jframe-javaeclipse) – pennstatephil Apr 21 '14 at 04:19
  • Thanks but that is just to display an image. I want to store/retrieve from a DB. That is also Eclipse IDE not Netbeans. Thanks though – pelagos Apr 21 '14 at 04:30
  • Java doesn't care what IDE you use. Also, you already said you're storing filenames, so you've answered that part. – pennstatephil Apr 21 '14 at 04:33
  • 1. What is the file path you are storing in the db 2. Where are your files located on the system? 3. Show the code where you are trying to load the image. 4. Do you need these images to embedded into the program, or is the program dependent on your specific file system? What problems exactly are you running into? – Paul Samsotha Apr 21 '14 at 04:47

1 Answers1

2

Things to consider, (if your image files are embedded resources, and not dependent on your file system)

  1. How will you store the image paths? You can simple just store the file names image.png

  2. What complete file path will you ultimately use. Have a set path (excluding the image file name), and based off your application file structure that is the path you will use, concatenated with with image file name. Example

    ProjectRoot
             src
                resources
                       images
                            image.png
    
    private static final String PATH = "/resources/images/";
    
  3. How will you load the image. You should read the image from the class path, using getClass().getResource() and you can just load the images to an ImageIcon and eventually adding them to JLabel. Example

    String location = rs.getString("Location");
    Image image = ImageIO.read(getClass().getResource(PATH + location));
    ImageIcon icon = new ImageIcon(image);
    JLabel label = new JLabel(icon);
    // add label to something.
    
Paul Samsotha
  • 205,037
  • 37
  • 486
  • 720
  • Thanks for that - very helpful. 1. I was planning on storing file names in the DB (i.e. image.png) as advised. 2. The path for the images will be a folder called 'images' s you have advised. 3. This is the bit I will work on as it was the unknown to me... – pelagos Apr 21 '14 at 05:36
  • Also I have read that Netbeans can only store .png files. Is this correct? Cause this would be a problem if so... – pelagos Apr 21 '14 at 05:39
  • 2
    No, why would you think that? It's not a matter of Netbenas storing it. It's a matter of will Java read it. This would also work fine with jpg, gif, .bmp .tiff. Not sure all the other supported files. Netbeans should have no affect on your application. It's simply a tool for development. – Paul Samsotha Apr 21 '14 at 05:40