How to convert ImageIcon to read/write it on db

Discussion in 'Java' started by carmelo, May 7, 2009.

  1. carmelo

    carmelo Guest

    Hi everybody,
    I'm working on a swing app which have to read/write from a MySql
    database, using JPA. The image is displayed on a JLabel. The problem
    is that for using data binding I need to create a converter to convert
    the image "to" and "from" the database...

    I wrote this converter, but I get runtime casting exceptions:


    public class ImageConverter extends Converter
    {
    //byte to image
    public ImageIcon convertForward(Object arg)
    {
    Image image = new ImageIcon( ( byte[] ) arg ).getImage()
    .getScaledInstance( 50, 50,
    Image.SCALE_SMOOTH );

    return new ImageIcon( image );
    }

    //image to byte
    public byte[] convertReverse(Object arg)
    {
    ByteArrayOutputStream bStream = new ByteArrayOutputStream();
    ObjectOutputStream oStream = null;
    try {
    oStream = new ObjectOutputStream(bStream);
    oStream.writeObject(arg);
    } catch (IOException ex) {
    Logger.getLogger(ConvertitoreImage.class.getName()).log
    (Level.SEVERE, null, ex);
    }
    byte[] byteVal = bStream. toByteArray();

    return new ImageIcon(byteVal);
    }

    }


    I hope you can help me. Thank you very much for your help!
     
    carmelo, May 7, 2009
    #1
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. terry

    JTable ImageIcon

    terry, Aug 5, 2003, in forum: Java
    Replies:
    2
    Views:
    4,106
    Roedy Green
    Aug 6, 2003
  2. Cheok Yan Cheng

    ImageIcon.paintIcon wont work in JApplet

    Cheok Yan Cheng, Aug 13, 2003, in forum: Java
    Replies:
    0
    Views:
    1,779
    Cheok Yan Cheng
    Aug 13, 2003
  3. Phil...

    How to close ImageIcon file

    Phil..., Oct 9, 2003, in forum: Java
    Replies:
    1
    Views:
    513
    William Brogden
    Oct 9, 2003
  4. David

    SplitPanel and ImageIcon

    David, Oct 16, 2003, in forum: Java
    Replies:
    2
    Views:
    496
  5. Replies:
    13
    Views:
    1,178
    RedGrittyBrick
    Jan 17, 2007
Loading...

Share This Page