Showing posts with label jfilechooser. Show all posts
Showing posts with label jfilechooser. Show all posts

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
*********************************************************************

Set file type in JFileChooser

Complete source code below will show you, how set file type that should be display in a JFileChooser. Source code below will display all directories and text files only.

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


import javax.swing.filechooser.FileFilter;

import javax.swing.JFileChooser;

import java.io.File;

public class SetJFileChooserFileType
{
public SetJFileChooserFileType()
{
//Create a file chooser from JFileChooser
JFileChooser fileChooser=new JFileChooser();

//Set file type that should be display by set JFileChooser's file filter
fileChooser.setFileFilter(new TypeOfFile());

//Show JFileChooser
int a=fileChooser.showDialog(null,"Open Text File");
}

//Class TypeOfFile
class TypeOfFile extends FileFilter
{
//Type of file that should be display in JFileChooser will be set here
//We choose to display only directory and text file
public boolean accept(File f)
{
return f.isDirectory()||f.getName().toLowerCase().endsWith(".txt");
}

//Set description for the type of file that should be display
public String getDescription()
{
return ".text files";
}
}

public static void main(String[]args)
{
SetJFileChooserFileType sjfcft=new SetJFileChooserFileType();
}
}


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

Create file chooser using JFileChooser

Source code below will show you, how to create a file chooser in java using JFileChooser class.

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


import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JButton;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import java.awt.FlowLayout;

public class CreateFileChooserUsingJFileChooser implements ActionListener
{
//Create file chooser using JFileChooser
JFileChooser fileChooser=new JFileChooser();

//Create a button that use to open file chooser
JButton button=new JButton("Click here to open file chooser");

//Create a JFrame that use to place created button
//JFrame title is ( Create file chooser using JFileChooser )
JFrame frame=new JFrame("Create file chooser using JFileChooser");

public CreateFileChooserUsingJFileChooser()
{
//add ActionListener to created button
button.addActionListener(this);

//set JFrame layout to FlowLayout
frame.setLayout(new FlowLayout());

//add button into JFrame
frame.add(button);

//set default close operation for JFrame
//So when you click window close button, this program will exit
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

//set JFrame size to :
//WIDTH : 500 pixels
//HEIGHT : 500 pixels
frame.setSize(500,500);

//Make your JFrame visible. So we can see it
frame.setVisible(true);
}

//Action that will take by button
public void actionPerformed(ActionEvent event)
{
//When you click the button, a file chooser will appear
if(event.getSource()==button)
{
//Show file chooser with title ( Choose a file )
fileChooser.showDialog(frame,"Choose a file");
}
}

public static void main(String[]args)
{
CreateFileChooserUsingJFileChooser cfcujfc=new CreateFileChooserUsingJFileChooser();
}
}


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

RELAXING NATURE VIDEO