public int showSaveDialog(Component parent)
where the parent is the parent component of the dialog, such as a JFrame.
Once the user typed a file name and select OK or Cancel, the method returns one of the following value:
◉ JFileChooser.CANCEL_OPTION: the user cancels file selection.
◉ JFileChooser.APPROVE_OPTION: the user accepts file selection.
◉ JFileChooser.ERROR_OPTION: if there’s an error or the user closes the dialog by clicking on the X button.
After the dialog is dismissed and the user approved a selection, you can use the getSelectedFile() methods to get the selected file:
Also, it's worth noting that before calling the showSaveDialog() method, you may want to set some options for the dialog. You can use the setDialogTitle(String) method to set a custom title text for the dialog and setCurrentDirectory(File) to set the directory where it will be saved.
Java Program to use JFileChooser in Swing
Here is a complete Java program that demonstrates how to use the file chooser class from the Swing package. You can just copy-paste the code and run in your favorite IDE like Eclipse, NetBeans, or IntelliJIDEA. You don't need to add any third-party library because swing comes along JDK itself.
package test;
import java.awt.BorderLayout;
import java.awt.FlowLayout;
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;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
/**
* Java program to demonstrate how to use JFileChooser for showing open file
* dialog and save file dialog in Java application.
*/
public class Test extends JFrame {
private final JButton upload = new JButton("Upload");
private final JButton save = new JButton("Save");
public Test() {
super("JFileChooser Example - Open Save File Dialong in Java");
setLayout(new FlowLayout());
upload.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent arg0) {
showSelectFileDialong();
}
});
getContentPane().add(upload);
setSize(300, 100);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
}
public static void main(String[] args) {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException
| InstantiationException
| IllegalAccessException
| UnsupportedLookAndFeelException e) {
}
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
Test t = new Test();
}
});
}
private void showSelectFileDialong() {
JFileChooser fileChooser = new JFileChooser();
fileChooser.setDialogTitle("Choose a File to upload");
// pass reference of your JFrame here
int response = fileChooser.showSaveDialog(this);
if (response == JFileChooser.APPROVE_OPTION) {
File selectedFile = fileChooser.getSelectedFile();
System.out.println("Save as file: "
+ selectedFile.getAbsolutePath());
}
}
}
How to run this Java Swing Program?
As I said, this program is complete in itself. You can just copy paste and run in your favorite IDE. Just make sure that you keep the name of the source file the same as the public class. If you use Eclipse then you don't even need to do that because Eclipse will automatically create the file with the same name.
You also don't need any third-party library because Swing is part of JDK. When you run this program it will start a Swing application and you will see a JFrame window as shown below
You can see the upload button, when you click on the button it will open the native file chooser option which will allow you to choose the file and folder you want to upload as shown in the following screenshot:
That's all about how to use the JFileChooser class in Java Swing application. It's one of the most useful and common components of Java Swing API and you will often find using it Java GUI application. Just make you understand the different API methods to customize the look and feel of the window and also how to get the name and other attributes of the chosen file to do the next action like saving into a database or any other directory.
0 comments:
Post a Comment