Thursday, August 29, 2013

JFileChooser

JFileChooser is a component that you can use to display an open file dialog to select a file or many files for opening or to display a save file dialog to select a directory in which you can save text to a file.
To display a open file dialog, you need to use the showOpenDialog(Component parent) method of the JFileChooser object. By using its showSaveDialog(Component parent) method, you will have a save file dialog. Below are constructors and commonly used methods of the JFileChooser class.

Constructors of the JFileChooser class:
-JFileChooser()
creates a file chooser pointing to the user's current directory.
-JFile(File directory)
creates a file chooser pointing to the specified File path.
-JFile(String directory)
creates a file chooser pointing to the specified string path.

Useful methods of the JFileChooser class:
-getSelectedFile():File
return a selected file object.
-getSelectedFiles():File[]
returns all selected file objects. The user is able to select many files from the file chooser when set the multiselection property to true by using the setMultiSelectionEnabled(boolean b) method.
-setFileFilter(FileFilter filter):void
sets the file filter to the file chooser.
-setMultiSelectionEnabled(boolean b):void
allows single or mutiple files selection.
-showOpenDialog(Component parent):int
displays an open file dialog.
-showSaveDialog(Component parent):int
displays a save file dialog.


Example:

import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import javax.swing.AbstractAction;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.filechooser.FileNameExtensionFilter;

class JFileChooserShow extends JFrame{
JTextArea txta;
JFileChooserShow(String title){
setTitle(title);
setSize(550,300);
setLayout(new FlowLayout());
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Container contpane=getContentPane();
contpane.setLayout(new BorderLayout());
JMenuBar mbar=new JMenuBar();
JMenu mfile=new JMenu("File");
JMenuItem itemnew=new JMenuItem(new AAction("New",new ImageIcon("d:/newicon.png")));
JMenuItem itemopen=new JMenuItem(new AAction("Open...",new ImageIcon("d:/openicon.png")));
JMenuItem itemsave=new JMenuItem(new AAction("Save as...",new ImageIcon("d:/saveicon.png")));
JMenuItem itemclose=new JMenuItem(new AAction("Exit",new ImageIcon("d:/closeicon.png")));
mfile.add(itemnew);
mfile.add(itemopen);
mfile.add(itemsave);
mfile.add(itemclose);
mbar.add(mfile);
setJMenuBar(mbar);
txta=new JTextArea(10,30);
txta.setLineWrap(true);
JScrollPane scroll=new JScrollPane(txta);
contpane.add(scroll,BorderLayout.CENTER);
setVisible(true);

}
class AAction extends AbstractAction{

AAction(String text){
super(text);

}

AAction(String text,Icon c){
super(text, c);

}
public void actionPerformed(ActionEvent e){
if(e.getActionCommand().equals("New"))
txta.setText("");
else if(e.getActionCommand().equals("Open..."))
showFileDiag(false);
else if(e.getActionCommand().equals("Save as..."))
showFileDiag(true);
else if(e.getActionCommand().equals("Exit"))
System.exit(0);

}
}

public void showFileDiag(boolean save){
JFileChooser chooser = new JFileChooser();
FileNameExtensionFilter filter = new FileNameExtensionFilter("Text (.*)","txt");
chooser.setFileFilter(filter);
chooser.setMultiSelectionEnabled(false);
int returnVal;

if(!save){
returnVal = chooser.showOpenDialog(this);
if(returnVal == JFileChooser.APPROVE_OPTION) {
File file=chooser.getSelectedFile();
readFile(file.toString());

}
}
else
{
returnVal = chooser.showSaveDialog(this);
if(returnVal == JFileChooser.APPROVE_OPTION) {
File file=chooser.getSelectedFile();
writeFile(file.toString());
}
}


}

public void readFile(String path){
txta.setText("");
try{
FileInputStream fs=new FileInputStream(path);
int data=0;
while((data=fs.read())!=-1){
char ch=(char)data;
txta.append(ch+"");
}
fs.close();
}catch(IOException ie){ie.printStackTrace();}
}
public void writeFile(String path){
try{
FileOutputStream fw=new FileOutputStream(path);
DataOutputStream ds=new DataOutputStream(fw);
String text=txta.getText();
ds.writeBytes(text);
ds.flush();
ds.close();
}catch(IOException ie){ie.printStackTrace();}
}

}

public class JFrameSwing {
public static void main(String[] args){
new JFileChooserShow("JFileChooser");
}
}

JFileChooser Open File dialog

JFileChooser Save File dialog

No comments:

Post a Comment