Thursday, July 8, 2010

Loading Image on JFrame using JFileChooser  

0 comments

JFileChooser is a very handy utility in java Swings to choose any particular file of an extension.
Sometimes in our applications we need to load images onto our main container .In java we can use either JFrame or JPanel to load images.Statically we can provide the path of image into our program,but most of times it is not feasible for us to give image path statically.For specifying image path dynamically we use JFileChooser which returns path of a selected image.You can also select the desierd file extensions for whom you are looking.In following Example it is elaborated how to use JFileChooser and using it how we can load an image on a container.

Example (Source code ):-

//imageloading.java

//Program developed by Vaibhav Pandey

import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;

public class imageloading extends JFrame
{
BufferedImage mImage;
String name,name1;
public imageloading()
{
JFrame frm=new JFrame("image loading test");
String source=filechoose();
File inputFile = new File(source);
try {
mImage = ImageIO.read(inputFile);
} catch (IOException ex) {
//Logger.getLogger(index.class.getName()).log(Level.SEVERE, null, ex);
}
JLabel lb=new JLabel(new ImageIcon(mImage));
frm.getContentPane().add(lb);
frm.show();
frm.pack();
}

String filechoose()
{
JFileChooser chooser = new JFileChooser();
chooser.setCurrentDirectory(new File("."));

chooser.setFileFilter(new javax.swing.filechooser.FileFilter() {
public boolean accept(File f) {
name = f.getName().toLowerCase();
return name.endsWith(".gif") || name.endsWith(".jpg")
|| name.endsWith(".jpeg") || f.isDirectory();
}

public String getDescription() {
return "Image files";
}
});

int r = chooser.showOpenDialog(this);

if (r == JFileChooser.APPROVE_OPTION) {
name1 = chooser.getSelectedFile().getAbsolutePath();
StringBuffer sb=new StringBuffer();
sb.append(name1);

int l=sb.length();
for(int i=0;i
{
if(sb.charAt(i)=='\\')
{
sb.insert(i, "\\");i++;
}
}
}
return name1;
}
public static void main(String a[])
{
new imageloading();
}
}

Output Windows:-

Following first widow shows the JFileChooser() instance opened and asking to
select a file of given extension,and in second window you can see the loaded image.


















Elaboration:-In the given code we have used the ImageIO class to read the image file whose file is being specified and consequently is buffered.Later this buffered image is set on a Label as a ImageIcon to display it on the frame.

JFileChooser() is used to choose the files of desired extension here FileFilter() method is used to filter the files on the basis of extensions .Here we have considered only .jpeg and .gif images.Since the '/' is treated as a escape sequence in Java thus in when we are fetching path name in variable 'name1' using method 'JFileChoose.getSelectedFile().getAbsolutePath()' ,in order to omit the escape sequence we embed '//' in the path when we have some path selected.

We have used 'JFileChooser.APPROVE_OPTION' because to verify that we have selected a legitimate file or not.Any queries and comments are most welcome.In Java JFileChooser is a nice utility ,use it and tell me about the tutorial.

What next?

You can also bookmark this post using your favorite bookmarking service:

Related Posts by Categories



Extension Factory Builder