I was wondering if there was some kind of J tool in the java swing library that opens up a file browser window and allows a user to choose a file. Then the ouput of the file would be the absolute path of the chosen file.
Thanks in advance,
io. FilenameFilter to search file in current directory and its subdirectory. The common methods getTargetFiles and printFiles are used to search files and print them.
JFileChooser has a method, getSelectedFile(). Which is a File. If you open the dialog with showSaveDialog() you should be able to get the File from that (file. getName()).
Click the Open a File button. Navigate around the file chooser, choose a file, and click the dialog's Open button. Use the Save a File button to bring up a save dialog. Try to use all of the controls on the file chooser.
The java Swing package is part of JavaTM Foundation Classes (JFC) . JFC contains many features that help in building graphical user interface in java . Java Swing provides components such as buttons, panels, dialogs, etc . JFileChooser is a easy and an effective way to prompt the user to choose a file or a directory .
Java Swing provides components such as buttons, panels, dialogs, etc . JFileChooser is a easy and an effective way to prompt the user to choose a file or a directory .
In swing applications, there would be a need to have a GUI component that allows users to specify a file to be opened or saved. There is no such built-in component in Swing, so we’re going to create a new one by extending JFileChooser class and some standard swing components.
For the integration of websites in Java Swing applications, we need a browser of course. That browser, in form of a GUI component, must be integrable in existing applications. Such a component is realized as a direct or indirect implementation of java.awt.component and will cover a certain area of the application interface.
You can use the JFileChooser class, check this example.
I ended up using this quick piece of code that did exactly what I needed:
final JFileChooser fc = new JFileChooser();
fc.showOpenDialog(this);
try {
// Open an input stream
Scanner reader = new Scanner(fc.getSelectedFile());
}
The following example creates a file chooser and displays it as first an open-file dialog and then as a save-file dialog:
String filename = File.separator+"tmp";
JFileChooser fc = new JFileChooser(new File(filename));
// Show open dialog; this method does not return until the dialog is closed
fc.showOpenDialog(frame);
File selFile = fc.getSelectedFile();
// Show save dialog; this method does not return until the dialog is closed
fc.showSaveDialog(frame);
selFile = fc.getSelectedFile();
Here is a more elaborate example that creates two buttons that create and show file chooser dialogs.
// This action creates and shows a modal open-file dialog.
public class OpenFileAction extends AbstractAction {
JFrame frame;
JFileChooser chooser;
OpenFileAction(JFrame frame, JFileChooser chooser) {
super("Open...");
this.chooser = chooser;
this.frame = frame;
}
public void actionPerformed(ActionEvent evt) {
// Show dialog; this method does not return until dialog is closed
chooser.showOpenDialog(frame);
// Get the selected file
File file = chooser.getSelectedFile();
}
};
// This action creates and shows a modal save-file dialog.
public class SaveFileAction extends AbstractAction {
JFileChooser chooser;
JFrame frame;
SaveFileAction(JFrame frame, JFileChooser chooser) {
super("Save As...");
this.chooser = chooser;
this.frame = frame;
}
public void actionPerformed(ActionEvent evt) {
// Show dialog; this method does not return until dialog is closed
chooser.showSaveDialog(frame);
// Get the selected file
File file = chooser.getSelectedFile();
}
};
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With