>

Vaibhav's Blog Space

Filter file(s) in JFileChooser

Wednesday May 07, 2008

JFileChooser is one of the most important components when we talk about Swing application from small to big size applications. Most of the time we write the code of JFileChooser on a button listener aka open a file on button name Open, so code will go :

open.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent e){
                int returnVal = fc.showOpenDialog(FileChooserFrame);
                //do action according to the value of returnVal     
                ................

But its always better to put a filter on a specific type of file which is required. Most of the time user don't required all types of files. Say, he need only .avi files or .mov files. Thens its a good idea to put a filter which will give only .avi or .mov files in the file chooser option. Month back, I had written one small code which do filtering for .java and .sh files and here it is:

import javax.swing.*;
import javax.swing.filechooser.FileFilter;
import java.io.File;

class JtregFilter extends FileFilter {

  public boolean accept(File f) {
    if (f.isDirectory())
      return true;
    String s = f.getName();
    int i = s.lastIndexOf('.');

    if (i > 0 && i < s.length() - 1)
      if (s.substring(i + 1).toLowerCase().equals("java" ) || s.substring(i + 1).toLowerCase().equals("sh" ))
        return true;

    return false;
  }

  public String getDescription() {
    return "*.java, *.sh";
  }
}

Now nothing need to do in listener except adding one more line :

open.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent e){
                fc.addChoosableFileFilter(new JtregFilter());
                int returnVal = fc.showOpenDialog(FileChooserFrame);
               //do action according to the value of returnVal    
               ...................

and we are done. Now JFileChooser will only show me folders and file with extension .java or .sh. Worthwhile to write a small code for user ease.


[3] Comments
Like this post? del.icio.us | furl | slashdot | technorati | digg

Online Java Output !

Friday Apr 25, 2008

Again one idea, implementation of which I am not able to find on internet. Most of the time for testing purpose we need to run small piece of codes basically the non-UI code. And we do it a lot when we prepare for certification exams like SCJP or SCJA. Some small tricky questions ! Not only this most of the time JDK version matters because one can't run generics code with JDK 1.4 backwards.

Why not to make a small web based tool, which takes the java file as an input from user and give option to user to select which JDK version is required probably by radio button(radio button is a nice name, its like radio in which you can select only one station at a time, I wonder why not television button :-) ) and we show the output of that java file on a JSP page or we can also write it somewhere on a file, as user demand, after all user is God :-).  For UI code, it will simple return a message like its a UI code and cant be displayed.

So, implementation is little like JFileChooser for selecting input file and for writing output file. Radio buttons for selection of JDK and thats it ! Rest my web server will take load of all JDK version and it will be the duty of code to run the java file on the appropriate JDK version.

Please give your useful comment on idea and also if there is anything exist like this. I would love to use it rather than writing :-).

[1] Comments
Like this post? del.icio.us | furl | slashdot | technorati | digg