0

I am totally new in Java. My teacher told me to develop a simple project which includes ADD,EDIT,DELETE etc features. I have done successfully all the features but when I add any image it doesn't fit into the jLabel which I take into a Desktop Pane. It displays as its actual size not according to the jLabel size. I tried a lot with the help of internet but its not working. Here is the code I've done so far,

private void btn_browseActionPerformed(java.awt.event.ActionEvent evt) {                                           
    JFileChooser chooser = new JFileChooser();
    chooser.showOpenDialog(null);
    File f = chooser.getSelectedFile();

    filename = f.getAbsolutePath();

    ImageIcon imageIcon = new ImageIcon(new ImageIcon(filename).getImage().getScaledInstance(lbl_img.getWidth(), lbl_img.getHeight(), Image.SCALE_DEFAULT));
    lbl_img.setIcon(imageIcon);
    txt_imgpath.setText(filename);

    try
    {
        File image  = new File(filename);
        FileInputStream fis = new FileInputStream(image);

        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        byte[] buf = new byte[1024];
        for(int readNum; (readNum=fis.read(buf))!=-1;)
        {
            bos.write(buf,0,readNum);
        }
        person_image=bos.toByteArray();
    }
    catch(Exception e)
    {
        JOptionPane.showMessageDialog(null, e);
    }
} 

private ImageIcon format = null;
String filename = null;
byte[] person_image = null;

I know it is a newbie type question and I am sorry for that but I really tried a lot but can't figure out what is the problem and how to solve it. Anyone please help me? :(

Chris Martin
  • 30,334
  • 10
  • 78
  • 137
Rifa Tabassum
  • 15
  • 2
  • 6
  • [For example](http://stackoverflow.com/questions/22679717/resizing-image-to-fit-in-jlabel/22680573#22680573) – MadProgrammer May 02 '17 at 03:26
  • @MadProgrammer By studying those examples is this the code, BufferedImage img = ImageIO.read(f); Image scaled = img.getScaledInstance(lbl_img.getWidth(), lbl_img.getHeight(), Image.SCALE_SMOOTH); ImageIcon icon = new ImageIcon(scaled); lbl_img.setIcon(icon); txt_imgpath.setText(filename); Its not working also.I really can't understand.Help me please. – Rifa Tabassum May 02 '17 at 06:13

0 Answers0