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
- 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.
[java]import java.awt.HeadlessException;
import java.awt.Image;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import javax.swing.ImageIcon;
import javax.swing.JFileChooser;
import javax.swing.JOptionPane;[/java]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.
[java]byte[] person_image = null;
String path;
String filename = null;[/java]5. Double click your browse button and insert the following codes below.
[java]try {
JFileChooser chooser = new JFileChooser();
chooser.showOpenDialog(null);
File f = chooser.getSelectedFile();
path = f + "";
filename = f.getAbsolutePath();
ImageIcon imgThisImg = new ImageIcon(new ImageIcon(filename)
.getImage().getScaledInstance(280, 187, Image.SCALE_DEFAULT));
jLabel1.setIcon(imgThisImg);
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 (HeadlessException | IOException e) {
JOptionPane.showMessageDialog(null, e, "Error", JOptionPane.ERROR_MESSAGE);
}[/java]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.
About The Display Image In Java
<figure class="wp-block-table is-style-stripes">
<table>
<tbody>
<tr>
<td><strong>Project Name:</strong></td>
<td>Display Image</td>
</tr>
<tr>
<td><strong>Language/s Used:</strong></td>
<td>JAVA</td>
</tr>
<tr>
<td><strong>Database:</strong></td>
<td>None</td>
</tr>
<tr>
<td><strong>Type:</strong></td>
<td>Desktop Application</td>
</tr>
<tr>
<td><strong>Developer:</strong></td>
<td>IT SOURCECODE</td>
</tr>
<tr>
<td><strong>Updates:</strong></td>
<td>0</td>
</tr>
</tbody>
</table><figcaption><em><strong>Display Image In Java</strong>- Project Information</em></figcaption></figure>Related Articles You May Like:
- How to Use Progress Bar in Java
- Create MySQL Connection in Java
- VAT Sales Receipt Calculator in Java
- Grade Average Calculator using Java
Frequently Asked Questions
How does this Java project work?
Built with Java Swing (NetBeans IDE) and MySQL backend via JDBC. Standard structure: JFrame designer to event handlers to DAO layer to MySQL. Login form for auth. Ready to extend for BSIT capstone scope.
What Java JDK and MySQL versions does this project require?
Most projects in this batch use Java JDK 8 or 11 with MySQL 5.7+ or MariaDB 10+. To run: install JDK (Adoptium / Oracle), install MySQL Server + MySQL Workbench, install NetBeans IDE (15+ supports modern JDK), open the project (.zip extracted folder), right-click + Open Project, add MySQL JDBC driver to Project Libraries, run.
How do I set up the database for this Java project?
Open MySQL Workbench (or phpMyAdmin if you have XAMPP), create a new empty database with the name specified in the project. Import the included .sql file via Server, Data Import in Workbench (or Import tab in phpMyAdmin). Update the connection class (usually DBConnection.java or DatabaseConnection.java) with your MySQL host, port, username, password, and database name.
Can I use this Java project for a BSIT capstone or thesis?
Yes, Java is one of the most accepted languages by Philippine BSIT panels. Extend it: add role-based access (admin/staff/customer login redirect), JasperReports printable reports, dashboards with JFreeChart, audit log, multi-branch support. Pair with Chapter 1-5 documentation matching your panel’s rubric.
Why am I getting ‘ClassNotFoundException: com.mysql.jdbc.Driver’ or ‘No suitable driver’?
Three common Java JDBC issues: (1) MySQL JDBC driver JAR not added to project Libraries. Right-click Project, Properties, Libraries, Add JAR/Folder, select mysql-connector-java-X.X.X.jar. (2) Wrong driver class name. Modern (8.0+) uses com.mysql.cj.jdbc.Driver, legacy (5.x) uses com.mysql.jdbc.Driver. (3) Connection URL missing serverTimezone parameter, add ?serverTimezone=UTC to the URL.
Where can I find more Java projects with source code?
Browse the Java Projects hub for the full library (120+ Java desktop systems). For modern Java web alternatives consider Spring Boot. For other desktop stacks see VB.NET Projects or C# Projects. For BSIT capstone idea lists see 150 Best Capstone Project Ideas.