Advertisement
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 | 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. |
Advertisement
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);
}
}
}
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);
}
}
}
Advertisement
Advertisement
Please check our latest addition
C#, PYTHON and DJANGO
Advertisement