Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Set the Location of the JFileChooser

How can we set the location of the JFileChooser window, I tried setLocation() and setBounds() methods but it doesn't works.

like image 520
Ram Avatar asked Feb 16 '10 04:02

Ram


People also ask

What is JFileChooser in Java?

JFileChooser is a part of java Swing package. 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 .

What are the commonly used constructors of JFileChooser?

Commonly used Constructors: Constructs a JFileChooser pointing to the user's default directory. Constructs a JFileChooser using the given File as the path. Constructs a JFileChooser using the given path.


1 Answers

Unfortunatley there is no trivial way to do it, because whenever you show the chooser, the internal createDialog method will set the location to center of parent.

One way to do is to subclass JFileChooser and override createDialog method like this:

   static class MyChooser extends JFileChooser {
        protected JDialog createDialog(Component parent)
                throws HeadlessException {
            JDialog dlg = super.createDialog(parent);
            dlg.setLocation(20, 20);
            return dlg;
        }
    }

Now you can directly uise MyChooser instead of JFileChooser. In above code I have hardcoded the location to 20, 20, but you can set it to whatever you want.

like image 56
Suraj Chandran Avatar answered Oct 08 '22 05:10

Suraj Chandran