Java File Chooser Example

Java File Chooser Example


Posted in : Java Posted on : April 26, 2012 at 7:21 PM Comments : [ 0 ]

In this tutorial you learn about java swing file chooser class

Java File Chooser Example

The presented tutorial given below is illustrates that that how to use the File Chooser in java. In the tutorial i am using swing JFileChooser class.

There are so many methods of JFileChooser class. such as setCurrentDirectory(), setMultiSelectionEnabled() etc.

Please look at the code given below

JavaFileChooserExample.java

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

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

public class JavaFileChooserExample extends JFrame {
	private static JButton button = new JButton("Open");
	private static JFileChooser fileChooser = new JFileChooser();

	public JavaFileChooserExample() {
		add(button);
		setSize(400, 200);
		this.setDefaultCloseOperation(EXIT_ON_CLOSE);
		setVisible(true);
	}

	public static void main(String[] args) {

		/* Enabling Multiple Selection */
		fileChooser.setMultiSelectionEnabled(true);

		/* Setting Current Directory */
		fileChooser.setCurrentDirectory(new File("C:\\Documents and Settings"));

		/* Adding action listener to open file */
		button.addActionListener(new ActionListener() {

			public void actionPerformed(ActionEvent event) {
				String command = event.getActionCommand();
				if (command.equals("Open")) {
					fileChooser.showDialog(new JavaFileChooserExample(),
							"File Chooser Example");
				}
			}
		});

		/* Running the Application */
		new JavaFileChooserExample();
	}
}


When you run this application it will display message as shown below:


 

Download Source Code

Go to Topic «PreviousHomeNext»

Your Comment:


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 

 
Tutorial Topics