0

I am fairly new to Java so my knowledge is limited. I have this assignment where I have to get data from an Access database and fill a dialogBox full of fields. I had no problem with typical fields, but I hit a dead end trying to make the attachment field work.

I've tried using the .getByte() methods I've seen on the web, and I don't quite grasp yet the Attachment uncanaccess class method. Can anyone help me or guide me in the right direction please? Here's some code for reference on how I've filled the other fields:

JTextField_cod_distrib.setText(result.getLong("Cod_distribuitor")+"");  
JCheckBox_in_stoc.setSelected(result.getBoolean("In_stoc"));
JTextField_pret.setText(result.getFloat("Pret")+"");     JTextField_denumire_produs.setText(result.getString("Denumire_produs")+"");
JTextField_cod_produs.setText(result.getInt("Cod_produs")+"");
JTextField_ambalaj.setText(result.getString("Ambalaj")+"");  
Edward B
  • 3
  • 3
  • According to the [UCanAccess website](http://ucanaccess.sourceforge.net/site.html#examples) you have to retrieve your attachment via ``ResultSet.getObject(columnName)``. Then you have a ``net.ucanaccess.complex.Attachment`` element. The [JavaDoc for this class](https://jar-download.com/artifacts/net.sf.ucanaccess/ucanaccess/4.0.4/documentation) shows a ``getData`` method for the bytes. But you didn't specify, what you want to do with the Attachment. – Sascha May 23 '19 at 09:34
  • I want to place it in a JLabel (like in my example). Basically I have a JDialogBox with fields and buttons, and I can cycle through every item in an database table, showing it's properties. I want to extract every item's attachment and place it in a JLabel inside that dialog box, like I've done above with the TextFields. How do I convert this Attachment object to a picture, and store it inside this label? – Edward B May 23 '19 at 10:01
  • Something like this should work: ``jlabel.setIcon(new ImageIcon(((Attachment)result.getObject("attachment")).getData()));`` – Sascha May 23 '19 at 10:39
  • @Sascha - Access allows multiple attachments per row in an `Attachment` field, so `getObject` returns an *array* of `net.ucanaccess.complex.Attachment` objects. – Gord Thompson May 23 '19 at 17:12
  • Please see my answer for that. – Sascha May 24 '19 at 07:54

1 Answers1

1

If you know that there is always exact one attachment in the array, you could do

jlabel.setIcon(new ImageIcon(getScaled(ImageIO.read(new ByteArrayInputStream(((Attachment[])result.getObject("attachment"))[0].getData())),120,120)));

Otherwise you will have to add a JLabel for every attachment:

JPanel attachmentPanel=new JPanel(new FlowLayout(FlowLayout.LEFT));
Attachment[] attachments=(Attachment[])result.getObject("attachment");
for(Attachment attachment:attachments) {
    Image original=ImageIO.read(new ByteArrayInputStream(attachment.getData()));
    attachmentPanel.add(new JLabel(new ImageIcon(getScaled(original,120,120))));
}
//add the attachmentPanel to your component

From https://docs.oracle.com/javase/tutorial/uiswing/examples/components/IconDemoProject/src/components/IconDemoApp.java

/**
     * 
     * Resizes an image using a Graphics2D object backed by a BufferedImage.
     * @param srcImg - source image to scale
     * @param w - desired width
     * @param h - desired height
     * @return - the new resized image
     */
    private BufferedImage getScaledImage(BufferedImage srcImg, int w, int h){
        double sw=srcImg.getWidth();
        double sh=srcImg.getHeight();
        double fw=w/sw;
        double fh=h/sh;
        if(fw<fh) w=(int)(sw*fh);
        else if(fh<fw) h=(int)(sh*fw);
        BufferedImage resizedImg = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB);
        Graphics2D g2 = resizedImg.createGraphics();
        g2.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);
        g2.drawImage(srcImg, 0, 0, w, h, null);
        g2.dispose();
        return resizedImg;
    }
Sascha
  • 1,320
  • 10
  • 16
  • Amazing. Works like a charm. The only issue is that the images aren't all the same size, so I need to scale them. Any idea how to do this with an Attachment object? – Edward B May 27 '19 at 14:14
  • Looking at the [Oracle Icon Tutorial Code](https://docs.oracle.com/javase/tutorial/displayCode.html?code=https://docs.oracle.com/javase/tutorial/uiswing/examples/components/IconDemoProject/src/components/IconDemoApp.java) I copied the ``getScaledImage`` method into my answer. – Sascha May 28 '19 at 08:12
  • Doesn't seem to be working. I get an error "Incompatible types: ImageIcon cannot be converted to Image". Here is the line of code: JLabel_poza.setIcon(new ImageIcon(getScaledImage(new ImageIcon(((Attachment[])result.getObject("Poza"))[0].getData()),120,120))); – Edward B May 31 '19 at 09:34
  • Oops, my bad. It should be an Image: ``JLabel_poza.setIcon(new ImageIcon(getScaled(ImageIO.read(new ByteArrayInputStream(((Attachment[])result.getObject("Poza"))[0].getData())),120,120)))``. – Sascha May 31 '19 at 09:43
  • Works like a charm. The only issue would be that not all of my images are square. How can I make the scaled height adapt to the width? – Edward B May 31 '19 at 15:07
  • I've added the proportional scaling part. – Sascha Jun 03 '19 at 07:09