As you have seen, the JFileChooser class provides the showOpenDialog method for displaying an open dialog and the showSaveDialog method for displaying a save dialog. The class has another method, showDialog , for displaying a file chooser for a custom task in a dialog.
JFileChooser provides a simple mechanism for the user to choose a file. For information about using JFileChooser , see How to Use File Choosers, a section in The Java Tutorial.
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()).
JFileChooser. setMultiSelectionEnabled(true) − To enable the multiple selection of file.
I am writing a java program that needs a file open dialog. The file open dialog isn't difficult, I'm hoping to use a JFileChooser
. My problem is that I would like to have a dual pane JFrame
(consisting of 2 JPanels
). The left panel would have a JList
, and the right panel would have a file open dialog.
When I use JFileChooser.showOpenDialog()
this opens the dialog box above all other windows, which isn't what I want. Is there any way to have the JFileChooser
(or maybe another file selection dialog) display inside a JPanel
and not pop-up above it?
Here is the code that I've tried, at this point it's very simplified. I'm only trying to get the JFileChooser
to be embedded in the JPanel
at this point.
public class JFC extends JFrame{
public JFC()
{
setSize(800,600);
JPanel panel= new JPanel();
JFileChooser chooser = new JFileChooser();
panel.add(chooser);
setVisible(true);
chooser.showOpenDialog(null);
}
public static void main(String[] args)
{
JFC blah = new JFC();
}
}
I have also tried calling chooser.showOpenDialog
with this
and panel
, but to no avail. Also, I have tried adding the JFileChooser
directly to the frame. Both of the attempts listed above still have the JFileChooser
pop up in front of the frame or panel (depending on which I add the JFileChooser
to).
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