How to browse for a file in java swing library?
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,
Asked by: Sawyer238 | Posted: 23-01-2022
Answer 1
You can use the JFileChooser class, check this example.
Answered by: Joyce682 | Posted: 24-02-2022Answer 2
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());
}
Answered by: Aston313 | Posted: 24-02-2022
Answer 3
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();
}
};
Answered by: Vanessa293 | Posted: 24-02-2022
Answer 4
In WebStart and the new 6u10 PlugIn you can use the FileOpenService, even without security permissions. For obvious reasons, you only get the file contents, not the file path.
Answered by: Lana362 | Posted: 24-02-2022Similar questions
A good Java library for network math
Closed. This question does not meet Stack Overflow guid...
java - Which library should I use to write an XLS from Linux / Python?
I'd love a good native Python library to write XLS, but it doesn't seem to exist. Happily, Jython does.
So I'm trying to decide between jexcelapi and Apache HSSF:
http://www.andykhan.com/jexcelapi/tutorial.html#writing
http://poi.apache.org/hssf/quic...
nlp - Fuzzy string search library in Java
Closed. This question does not meet Stack Overflow guid...
What is the best Java OXM library?
Even though I've been a developer for awhile I've been lucky enough to have avoided doing much work with XML. So now I've got a project where I've got to interact with some web services, and would like to use some kind of Object-to-XML Mapping solution.
The only one I'm aware of is JAXB. Is that the best to go with? Are there any other recommendations?
One catch - I'm stuck using Java 1.4, so I can't do an...
Java Chart library for OLAP
Closed. This question does not meet Stack Overflow guid...
Using a java library from python
I have a python app and java app. The python app generates input for the java app and invokes it on the command line.
I'm sure there must be a more elegant solution to this; just like using JNI to invoke C code from Java.
Any pointers?
(FYI I'm v. new to Python)
Clarification (at the cost of a long question: apologies)
The py app (which I don't own) takes user input in the form of...
Is there a Java library to access the native Windows API?
Is there a Java library to access the native Windows API? Either with COM or JNI.
PDF to text tool or Java library?
java - How Can I Add the Apache POI Library in and Eclipse Project?
I download the Apache POI from apache.org but don't know how use it with one of my existing project in Eclipse.
Do you know of a Java library to access the native linux api?
Do you know of a Java library to access the native linux api?
I guess something like this must use JNI. So be it.
Still can't find your answer? Check out these amazing Java communities for help...
Java Reddit Community | Java Help Reddit Community | Dev.to Java Community | Java Discord | Java Programmers (Facebook) | Java developers (Facebook)