Results 1 to 3 of 3
  1. #1
    Join Date
    May 2014
    Posts
    3
    Rep Power
    0

    Default Load an image from the database and show in a Java Label (setting as icon)

    Hi!
    I've an image memorized into a databas in tinyblob form.
    I call it in java in this mode:

    Product p = new Product();
    ...
    p.setImgByte(rs.getByte("ImmagineByte"));
    (p is a javabean, and imgByte is a byte format variable)

    Once done that, what i've to do for show it into a swing label?

    I've already setted an image retrieving it from an url in this mode
    PHP Code:
     Image img = ImageIO.read(new URL(a.getImmagine()));
                 Image resizedImage = img.getScaledInstance(imgContainer.getWidth(), imgContainer.getHeight(), Image.SCALE_SMOOTH);
                 imgContainer.setIcon(new ImageIcon(resizedImage));
    But i don't know how to do the same thing with a byte format image inteasd of the url

  2. #2
    kneitzel is offline Senior Member
    Join Date
    Feb 2014
    Posts
    447
    Rep Power
    1

    Default Re: Load an image from the database and show in a Java Label (setting as icon)

    Hi,
    So you have a byte array or Blob instance.
    If you have a Blob, you call getBytes to get an byte array.
    You create a ByteArrayInputStream instance and then you call
    ImageIO.read with that parameter.

    Konrad

  3. #3
    Join Date
    May 2014
    Posts
    3
    Rep Power
    0

    Default Re: Load an image from the database and show in a Java Label (setting as icon)

    EDIT:

    I done this:
    PHP Code:
    int blobLength = (int) a.getImmagineByte().length();  
    byte[] blobAsBytes = a.getImmagineByte().getBytes(1, blobLength);
    
    //release the blob and free up memory. (since JDBC 4.0)
    a.getImmagineByte().free();
    ByteArrayInputStream bai=new ByteArrayInputStream(blobAsBytes);
    
    Image img;
    img = ImageIO.read(bai);
    imgContainer.setIcon(new ImageIcon(img));
    Error:
    Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
    at javax.swing.ImageIcon.<init>(ImageIcon.java:228)
    at Frame.ModalitaProdotto.popolaFrame(ModalitaProdott o.java:124)
    at Frame.ModalitaProdotto.btnNextActionPerformed(Moda litaProdotto.java:529)
    .......
    Last edited by American horizon; 05-31-2014 at 05:35 PM.

Similar Threads

  1. Setting Array List to label within for loop
    By ajs1351 in forum New To Java
    Replies: 5
    Last Post: 11-19-2011, 07:10 PM
  2. Replies: 0
    Last Post: 11-07-2011, 01:51 PM
  3. Show/Hide Label with JToggleButton
    By ntagrafix in forum AWT / Swing
    Replies: 4
    Last Post: 11-04-2009, 02:19 AM
  4. [SOLVED] Setting a icon
    By sirwiggles in forum New To Java
    Replies: 4
    Last Post: 02-16-2009, 08:41 PM
  5. How to set an Icon in a Label?
    By Soda in forum New To Java
    Replies: 2
    Last Post: 12-07-2007, 12:38 PM

Posting Permissions

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