Display Image Using Java File Chooser

0
57

This tutorial is all about Display Image Using Java File Chooser. This tutorial will teach you on how to load a selected image using Java file chooser and display it to a Java label element. This program will use HeadlessException, Image, ByteArrayOutputStream, File, FileInputStream, IOException, ImageIcon, JFileChooser, and JOptionPane libraries. The following libraries are already bundles with Netbeans 8.0.2 and you don’t need to download it again from the internet.

Display Image Using Java File Chooser Steps

  1. Add a new JFrame form inside your source package.

2. Design your form just like the image below. The form uses jPanel, jLabel, and jButton elements located in our tool pallet.

3. Add the following codes above your main class. The following codes will access the required libraries for the program.

  1. import java.awt.HeadlessException;
  2. import java.awt.Image;
  3. import java.io.ByteArrayOutputStream;
  4. import java.io.File;
  5. import java.io.FileInputStream;
  6. import java.io.IOException;
  7. import javax.swing.ImageIcon;
  8. import javax.swing.JFileChooser;
  9. import javax.swing.JOptionPane;

4. Add the following codes inside your main class. The following codes are the variable declaration use in the program. person_image is a variable with byte data type, path and filename are variables with string data types.

  1. byte[] person_image = null;
  2. String path;
  3. String filename = null;

5. Double click your browse button and insert the following codes below.

  1. try {
  2. JFileChooser chooser = new JFileChooser();
  3. chooser.showOpenDialog(null);
  4. File f = chooser.getSelectedFile();
  5. path = f + "";
  6. filename = f.getAbsolutePath();
  7. ImageIcon imgThisImg = new ImageIcon(new ImageIcon(filename)
  8. .getImage().getScaledInstance(280, 187, Image.SCALE_DEFAULT));
  9. jLabel1.setIcon(imgThisImg);
  10. File image = new File(filename);
  11. byte[] buf = new byte[1024];
  12. for (int readNum; (readNum = fis.read(buf)) != -1;) {
  13. bos.write(buf, 0, readNum);
  14. }
  15. person_image = bos.toByteArray();
  16. } catch (HeadlessException | IOException e) {
  17. JOptionPane.showMessageDialog(null, e, "Error", JOptionPane.ERROR_MESSAGE);
  18. }

6. Run your program and the output should look like the image below. After the user click the browse button, the Java File Chooser dialog appear and you need to browse and select a specific image/picture to load in Java label.

There are two executions happen from the source code above, the loading of image into Java label and the conversion of image path into byte.

When the users select an image using java file chooser, the selected image will directly display into java label and also converted the image path into byte. We will use the output byte when we save or insert the image into MySQL database.

After completing this tutorial, you are now learned and understand on how to load a specific image or picture into java label and also converting the image into byte.

Related Articles You May Like:

Facebook Comments

LEAVE A REPLY

Please enter your comment!
Please enter your name here