JFileChooser embedded in a JPanel

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).


Asked by: Sarah904 | Posted: 21-01-2022






Answer 1

JFileChooser extends JComponent and Component so you should be able to add it directly to your frame.

JFileChooser fc = ...
JPanel panel ...
panel.add(fc);

Answered by: Miranda713 | Posted: 22-02-2022



Answer 2

To access the "buttons" in the file chooser, you will have to add an ActionListener to it:

fileChooser.addActionListener(this);
[...]

public void actionPerformed(ActionEvent action)
{
    if (action.getActionCommand().equals("CancelSelection"))
    {
        System.out.printf("CancelSelection\n");
        this.setVisible(false);
        this.dispose();
    }
    if (action.getActionCommand().equals("ApproveSelection"))
    {
        System.out.printf("ApproveSelection\n");
        this.setVisible(false);
        this.dispose();
    }
}

Answered by: Arthur186 | Posted: 22-02-2022



Answer 3

If you are adding the JFileChooser on the fly, you will need to call revalidate().

Steve's answer is correct. You can add a JFileChooser to other containers.

Answered by: Miranda414 | Posted: 22-02-2022



Answer 4

To Johannes: thanks for your useful snippet.

Instead of "ApproveSelection" and "CancelSelection" I used the defined constants JFileChooser.APPROVE_SELECTION and JFileChooser.CANCEL_SELECTION

Answered by: Michael243 | Posted: 22-02-2022



Similar questions

java - JFileChooser use within JApplet

Can a JApplet use a JFileChooser so that the user can select a file on his hard-drive? Or would this violate Java applet security? (I'm assuming that the default security settings are being used. I don't want to ask my users to grant me extra permissions.)


interface - How do I setup up JFileChooser for single click behavior in java Swing?

How do I change the JFileChooser behavior from double-click selection to single-click selection mode? I'm developing an application to run with either a single-click interface (nothing requires a double-click, just like the KDE interface mode) or a double-click interface (the default Windows interface mode or the regular GNOME interface mode). I want the Java application to behave just like the rest of...


java - JFileChooser hangs sometimes

I am running into the problem of the "hanging JFileChooser" as described in the following threads: http://forums.sun.com/thread.jspa?threadID=5309960 http://forums.sun.com/thread.jspa?threadID=724817


java - JFileChooser for Python?

I was wondering if there is something similar to Java's JFileChooser for Python? JFileChooser is a graphical front end to choose a file. Preferably something that is already with Python. Maybe with Tkinter.


java - Alternative to JFileChooser

I've a request to make some changes to a little applet that currently use a JFileChooser. One of the main complaints is that the file chooser is a pain in the ass to use because it behaves differently than the native widget, especially for navigating up to the root level. So, knowing that and all the other issue JFileChooser suffer (like the zip file caching on windows...), I was wondering that a viable alterna...


java - JFileChooser on OS X

JFileChooser looks nothing like the native widget. I seem to remember reading some hack to get it look like the native widget but searching for it know i can't seem to find it again i came across posts that suggest using java.awt.FileChooser but that class does not seem to be in the distribution. How can i make JFileChooser make look like the native widget?


java - Part of path returned from Directories Only JFileChooser is sometimes duplicated

In my application, I want the user to be able to select a directory to store stuff in. I have a text field that I'm using to display the directory they've chosen. If they just click on a directory (don't browse it), everything is fine. However, if they double click on the directory and look inside it, the directory name is duplicated. Ex. They're in the home directory, single click the folder Desktop...path returne...


java - How can we set the exit button of JFileChooser?

we use setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE) for JFrame and those classes that implements JFrame. what can we do like this for JFileChooser?(the window that suddenly when you push browse button will pups up)


java - JFileChooser for directories on the Mac: how to make it not suck?

The JFileChooser in "directories only" mode on the Mac has two serious, crippling problems: 1) You cannot create directories with it 2) You cannot switch drives This is rather a huge problem for my installer app. As far as I can tell, Apple provides no way around this problem, you can't even activate the non-native directory chooser ... so the only alternative is to find a free/open source pure-Java...


java - JFileChooser browsing a remote file system

I am trying to Implement a JFileChooser that allows a user to select files on a remote system via ftp. Everything I've read says this can be accomplished by extending FileSystemView so that all the file system methods (getFiles, etc) are overridden and routed across ftp. I haven't had any luck implementing this, as FileSystemView appears to still be calling local functions. Can anyone confirm that t...






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)



top