C
carmelo
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!
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!