简体   繁体   中英

How to view pictures using JFilechooser and Jlabel

Hi guys I want to ask how can I view Images using Jlabel and attach that image using Jfilechooser?

what code do I use so that when I attached a image it will automatically shown in the Jlabel?

Thanks in advance

Here is my JFileChooser code:

JFileChooser image=new JFileChooser();
    image.showOpenDialog(null);
    File f=image.getSelectedFile();
    filename=f.getAbsolutePath();
    txtFilename.setText(filename);

    try{



        File images=new File(filename);
        FileInputStream fis=new FileInputStream(images);
        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(IOException e){
        JOptionPane.showMessageDialog(null,e);

    }
Image image = null;
    try {
      JFileChooser fc=new JFileChooser();
      fc.showOpenDialog(null);
      File f=fc.getSelectedFile();

        image = ImageIO.read(f);      

    } catch (IOException e) {
    }

    // Use a label to display the image
    JFrame frame = new JFrame();
    JLabel label = new JLabel(new ImageIcon(image));
    frame.getContentPane().add(label, BorderLayout.CENTER);
    frame.pack();
    frame.setVisible(true);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM