4

I'm using Java Swing (GUI) and I want to add a button to my project for opening files.

I don't like the JFileChooser since it opens a small window for browsing through the files of the directories. Can I use something else instead of the JFileChooser under Java Swing?

I've tried to use elements of SWT but it didn't work, meaning is the use of the button object and then use it inside the JFrame, but that failed, so I guess SWT and Swing don't mix together?

Here is the example of Java Swing with JFileChooser and I'm looking for something like this to put in my JFrame.

Windows file dialog

Nayuki
  • 17,911
  • 6
  • 53
  • 80
JAN
  • 21,236
  • 66
  • 181
  • 318

2 Answers2

6

JFileChooser with the native PLAF seems to fulfill the stated requirement.

Native PLAF file chooser

import javax.swing.JFileChooser;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;

public class NativeFileChooser {

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(
                            UIManager.getSystemLookAndFeelClassName());
                } catch(Exception e) {
                    e.printStackTrace();
                }
                JFileChooser jfc = new JFileChooser();
                jfc.showOpenDialog(null);
            }
        });
    }
}

Still not quite to your liking? Then you might start with this one & change it to need:


..so I guess SWT and Swing don't mix together?

It is generally not a good idea to mix Swing/AWT/SWT components in the same top-level container. It is not a problem to open an AWT FileDialog over a Swing based JFrame since they are both top-level containers. I am pretty sure the same would apply to Swing/SWT or AWT/SWT.

Community
  • 1
  • 1
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
  • How can I use this code without the `main` ? I've tried to remove this but the code won't compile without it . Thanks !! – JAN Jun 05 '12 at 16:54
  • *"How can I use this code without the main?"* Unless you have a tricky IDE that inserts a default (invisible) `main` method for you, it is necessary to either have at least one `main` or develop an applet, to get a Swing GUI on-screen. I don't recommend the applet, so stick with understanding how to get code to work from a `main`. – Andrew Thompson Jun 05 '12 at 16:56
  • I need to pick a file and then parse it , in short , the process of choosing the file is only the beginning of the program , hence the `main` makes it difficult . Should I post the entire code in another question ? thank again . – JAN Jun 05 '12 at 17:00
  • *"..post the entire code in another question.."* 1) Yes, another question, but 2) For better help sooner, post an [SSCCE](http://sscce.org/) (rather than the 'entire code'). – Andrew Thompson Jun 05 '12 at 17:33
  • FYI: That works on Windows, but not on OS X. Under OS X the `FileDialog` shows the standard 'Open file' window (including a sidebar) while the `JFileChooser` does not have a side bar. And I am afraid that under Linux you end up with the GTK `JFileChooser` which is quite frankly un-usable and has nothing in common with a decent file chooser – Robin Jun 05 '12 at 17:46
  • @Robin *"That works on Windows, but not on OS X."* Define 'works'. I would not try to foist a Windows style file chooser on the OS X users. My experiences with the GTK chooser are from a while ago & I'm a bit hazy, maybe offering the user whatever PLAF they want would be best. They might prefer the Nimbus style file chooser. It is a pity that the AWT `FileDialog` is not more powerful/configurable. – Andrew Thompson Jun 05 '12 at 17:52
  • I mean that the native Look-and-feel (=Aqua) `JFileChooser` on OS X is not the same as the standard 'Open a file' dialog you get in the standard OS X applications. The `FileDialog` on the other hand looks the same. – Robin Jun 05 '12 at 18:09
3

If you do not need the flexibility of the JFileChooser, you can opt for the FileDialog which uses the native OS file dialog. See also Code ranch topic and this answer on SO

Community
  • 1
  • 1
Robin
  • 36,233
  • 5
  • 47
  • 99