Conrtibuted by: Jay Mahadeokar.

Here I demonstrate a small module project that will enable us to open Images within NetBeans using Java Advanced Imaging API.



There is a similar tutorial available at which demonstrates how we can view images in the MyPictures folder through NetBeans using the NetBeans TopComponent, ExplorerAPI, FileSystemsAPI. Thanks to Kunal Modi & Shantaram Waingankar!.

In the following tutorial we will take a completely different approach and use the Java Advanced Imaging(JAI) API (you might be knowing about it)to handle images and a simple JFileChooser to browse and select them.

Point worth noting is that JAI allows you to select all types and kinds of images. And the functionality can be easily extended to zooming, cropping, rotating, finding out RGB components of image ,saving it and so on as I have demonstrated in the ImagePlugin that i have hosted on NetBeans plug-in portal.

1. Introduction

We often have to include image files in our projects. To select a proper image and its suitability we need the help of image viewer. But it would be nice if we can open the images from within the IDE. We will see how easily it can be done using NetBeans Module development and the Java Advanced Imaging API.(JAI)

About JAI:

JAI is intended to support image processing using the Java programming language as generally as possible. At the same time, JAI presents a simple programming model that can be readily used in applications without a tremendous mechanical programming overhead or a requirement that the programmer be expert in all phases of the API’s design. JAI encapsulates image data formats and remote method invocations within a reusable image data object, allowing an image file, a network image object, or a real-time data stream to be processed identically. Thus, JAI represents a simple programming model while concealing the complexity of the internal mechanisms. Detailed Information on JAI is present on the Official JAI Site

2. Resources Needed:

  1. NetBeans IDE 6.0/ 6.1Beta
  2. JAI API

Download JAI API Builds Install JAI API in appropriate directory. Make note of the path. We will need to add the JAR files in our libraries of project if it is not installed in proper directory of NetBeans.

You may want to refer this beginners tutorial before proceeding

3. Setting up Display:

I. Create a new NetBeans module project.

Name it as ImageModule.

II. Create a new Window Component.

  1. Set Window position as Editor.
  2. Give Class Name Prefix as: ImageFrame
  3. Select a suitable icon (16 x 16).
  4. Click Finish.

You will have the following in your project tab


III. Go to ImageFrameTopComponent and add an internalFrame to it from the palette.

Rename it as ImageFrame.


IV. Add a MenuBar to the Frame. Delete the Edit menu button. Rename File Menu Button as Image.

V. Add a MenuItem to it as shown.

Edit its text to Open and set the variable name as openMenu.


VI. Add an ActionPerformed event Handler to the Menu button.

OpenMenu >> Right Click >>Events >> ActionPerformed


4. Putting code in Places:

i. Import the following packages:

import javax.swing.JFileChooser;
import java.awt.BorderLayout;
import java.awt.Container;
import javax.swing.JScrollPane;

ii. Add private variables:

//To select the path of image file
private File file;
//To load the image file
private PlanarImage srcImage = null;
//To display the image file
private DisplayJAI srcdj = null;
private JScrollPane srcScrollPaneImage = null;
//To choose file by browsing
private JFileChooser FileChooser = new JFileChooser();
//This flag will be set once image is loaded for the first time
public boolean flag = false;

iii. Add the following method to the class:

public void displayimage(File path )

// Load the image which file name was passed as the first argument to the
// application.
srcImage = JAI.create("fileload", path.getPath());
//When image is loaded for first time...
if(flag == false )
// Get the JFrame's ContentPane.
Container contentPane = ImageFrame.getContentPane();
contentPane.setLayout(new BorderLayout());
// Create an instance of DisplayJAI.
srcdj = new DisplayJAI(srcImage);
// Add to the JFrame's ContentPane an instance of JScrollPane containing the
// DisplayJAI instance.
srcScrollPaneImage = new JScrollPane(srcdj);
ImageFrame.setVisible(true); // show the frame.
flag = true;
//If any image was already loaded before

iv. In the OpenMenu Action Performed Handler add the following code. It wil launch the JFileChooser dialog box so as to browse for image to be selected.

int returnVal = FileChooser.showOpenDialog(this);
if (returnVal == JFileChooser.APPROVE_OPTION)
//Initialise file object
file = FileChooser.getSelectedFile();
//Display the image


V. Build and Run the project.

1. Output:

A new instance of the IDE will be launched. You can open the Image Frame as shown.


2. Choose the image using the file chooser. Image will get displayed as shown.


6. Summary:

We saw how easily images can be loaded in NetBeans using JAI. We also learned to use the jFileChooser dialog box to add browsing functionality to our project.

Not logged in. Log in, Register

By use of this website, you agree to the NetBeans Policies and Terms of Use. © 2012, Oracle Corporation and/or its affiliates. Sponsored by Oracle logo