< Prev
Next >



JFileChooser class




JFileChooser class is used to provide a GUI(Graphical User Interface) to user, using which a user selects a file by navigating through the file system.




Constructors of JFileChooser


Constructor Description
public JFileChooser() Creates a JFileChooser window, pointing to the default directory of the system.
public JFileChooser(File currentDirectory) Creates a JFileChooser window, pointing to the path specified by currentDirectory.






Methods of JFileChooser class


Methods Description
public File getSelectedFile() Gets the selected file, pointed by the File object.
public File getCurrentDirectory( Gets the current directory pointed by File object.
public void setFileFilter(FileFilter filter) Sets the file filter.





Using JFileChooser.


import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.io.*;


public class FileChooser3
{
public static void main(String... ar)
{
SwingUtilities.invokeLater(new Runnable() {
public void run()
{
new A();
}
});

}//Closing the main method
}//Closing the class Combo


class A implements ActionListener
{
JFrame jf;
JMenuBar menuBar;
JMenu menu1;
JMenuItem mItem1, mItem2, mItem3;
JFileChooser fc;

A()
{
jf = new JFrame("Displaying JFileChooser");

//Creating a menu bar
menuBar= new 	JMenuBar();

//Creating first menu
menu1 = new JMenu("File");
mItem1 = new JMenuItem("New");
mItem2 = new JMenuItem("Open");
mItem3 = new JMenuItem("Exit");

//Adding menu items to the  menu
menu1.add(mItem1);
menu1.add(mItem2);
menu1.add(mItem3);

//Adding our menu  to the menu bar
menuBar.add(menu1);

mItem2.addActionListener(this);

jf.add(menuBar, BorderLayout.NORTH);
jf.setSize(370,270);
jf.setVisible(true);
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}

public void actionPerformed(ActionEvent ae)
{
if(ae.getActionCommand().equals("Open"))
{

//Creating an object of JFileChooser
fc= new JFileChooser("Select a file");

//Showing a file chooser dialog window
int returnValue = fc.showOpenDialog(jf);
fc.setSize(200,200);
fc.setVisible(true);
}

}
}

When you run the code, you are presented a window that shows a menu and its menu items -

Figure 1


When you click on this menu item named Open, a new window opens up which lets us select and search for the file present in your computer.

Figure 2

Note : By default, this window points to the "Document and Settings" directory, which is default user directory in Microsoft Operating System.




File filtering with JFileChooser.




import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import javax.swing.filechooser.FileNameExtensionFilter;


public class FileChooser3
{
public static void main(String... ar)
{
SwingUtilities.invokeLater(new Runnable() {
public void run()
{
new A();
}
});

}//Closing the main method
}//Closing the class Combo


class A implements ActionListener
{
JFrame jf;
JMenuBar menuBar;
JMenu menu1; 
JMenuItem mItem1, mItem2, mItem3;
JFileChooser fc;
JPanel jp;
JLabel label1, label2, label3;

A()
{
jf = new JFrame("Handling events in JFileChooser");

//Creating a menu bar
menuBar= new 	JMenuBar();

//Creating first menu
menu1 = new JMenu("File");
mItem1 = new JMenuItem("New");
mItem2 = new JMenuItem("Open");
mItem3 = new JMenuItem("Exit");

//Adding menu items to the  menu
menu1.add(mItem1);
menu1.add(mItem2);
menu1.add(mItem3);

//Adding our menu  to the menu bar
menuBar.add(menu1);

mItem2.addActionListener(this);

jp = new JPanel(new FlowLayout(FlowLayout.LEFT));
label1 = new JLabel("");
label2 = new JLabel("");
label3 = new JLabel("");

//Adding labels to panel
jp.add(label1);
jp.add(label2);
jp.add(label3);

//Adding menu and panel to frame
jf.add(menuBar, BorderLayout.NORTH);
jf.add(jp,BorderLayout.CENTER);
jf.setSize(310,270);
jf.setVisible(true);
}

public void actionPerformed(ActionEvent ae)
{
if(ae.getActionCommand().equals("Open"))
{
 
File path = new File("D://");

FileNameExtensionFilter filter = new FileNameExtensionFilter("Only showing txt and .jpeg files", "txt", "jpg");

//Creating an object of JFileChooser
fc= new JFileChooser(path);

//Setting the filter to only show files with specific format in JFileChooser window
fc.setFileFilter(filter);

//Showing a file chooser dialog window
int returnValue = fc.showOpenDialog(jf);

if(returnValue == JFileChooser.APPROVE_OPTION)
{
File file = fc.getSelectedFile();

label1.setText("File you've selected - "+ file.getName());
label2.setText("Path to the file - " + file.getAbsolutePath());
label3.setText("Path to parent directory -" +fc.getCurrentDirectory());
}

//fc.setSize(200,200);
fc.setVisible(true);
}

}

}

When you run the code, you are presented a window that shows a menu and its menu items -

Figure 3


When you click on this menu item named Open, a new window opens up which shows you contents of D: Drive, fitering to its folder and files with extension .txt and .jpeg. We have selected an image file with the name IMG_1326.jpeg

Figure 4


After selecting this file, when we click on Open button, the information about this file displayed to us in the window


Figure 5




Please share this article -

Facebook Google Pinterest Reddit Tumblr Twitter



< Prev
Next >
< JScrollBar
Inner Class >
Please subscribe our social media channels for notifications, we post a new article everyday.

Decodejava Google+ Page Decodejava Facebook Page  DecodeJava Twitter Page

Coming Next
-
C++ and Python

Ad2