Open file using JFileChooser

Complete source code below will show you, how to open file using JFileChooser.

*********************************************************************
COMPLETE SOURCE CODE FOR : OpenFileUsingJFileChooser.java
*********************************************************************


/*
*Desktop class only can get when you use JDK 6
*/
import java.awt.Desktop;

import javax.swing.JFileChooser;

import java.io.File;

public class OpenFileUsingJFileChooser
{
public static void main(String[]args)
{
//Create a file chooser
JFileChooser fileChooser=new JFileChooser();

//File chooser will appear in no windows parent
int a=fileChooser.showOpenDialog(null);

//Action that will take when user click open button
if(a==JFileChooser.APPROVE_OPTION)
{
//Get file that want to open
File fileToOpen=fileChooser.getSelectedFile();

try
{
//Open file using suitable program on computer.
//You don't need to tell what program to use.
//Java will select default program that your
//computer use to open the file.
//In windowsXP you can see what
//program that your computer use to open
//a file by right click on file,
//choose properties and see at
//Opens with : ProgramNameToOpenTheFile
Desktop.getDesktop().open(fileToOpen);
}
catch(Exception exception)
{
System.out.println("Problem occour when to open the file");
}
}
}
}


*********************************************************************
JUST COMPILE AND EXECUTE IT
*********************************************************************

8 comments:

  1. Perfect example!! thanks

    ReplyDelete
  2. dats excellent....

    ReplyDelete
  3. thank u so so much....

    ReplyDelete
  4. woa, u be bullet! i don de look 4 dis stuf teytey. u too much, bros sey!

    ReplyDelete
  5. Thank U very much, I owe you big.

    ReplyDelete
  6. Really Nice. I have been searching since 2000 April as I know this type of opening a text file in visual fox pro. Thanks very much L.Sankaranarayanan

    ReplyDelete
  7. Very good! Thank you!

    ReplyDelete

Leave a comment

Related Posts Plugin for WordPress, Blogger...

I Also Can Create T-Shirt Logo For You