Em Ae
Em Ae

Reputation: 8704

Opening FileChooser Dialog

this is my first ever Swing project and I barely know anything related to Java and GUI. After struggling with SWT (Eclipse), I decided to switch to Netbeans for Swing app (I don't want to go for netbeans platform atm). so I might nag you guys a bit while my project is going on.

My first question is how can I open a JFileChooser dialog to select a specific directory? That's what I have done:

IN SWING DESIGNER MODE

IMPORTANT The question I am asking is not how to use JFileChooser. I am asking how to use it with Swing Designer in Netbeans. When i drop it from the Palette, it is always shown in the designer.

P.S: After lots of struggle I decided to go with Swing. I was not even willing to go for Java based GUI but I had not choice because Java has better underlying api for the project I am working on and now building a basic GUI sucks!

Upvotes: 0

Views: 4668

Answers (4)

Naveed Quadri
Naveed Quadri

Reputation: 508

I know this question has an accepted answer but If you want to use JFileCHooser from the netbeans design view, goto navigator, right click on other components -> add from palatte. JFileChooser will be added to your panel/frame and you can configure it using the netbeans properties window

Upvotes: 0

alain.janinm
alain.janinm

Reputation: 20065

You are making a confusion I think, the File Chooser you use is from the Swing Window, you can't add or attach this one to a button.

The easiest way to proceed :

Properties of the button -> Events -> actionPerformed

Add an event (click on the arrow to choose default or ... to custom the method name

Netbeans will automatically switch to Source mode.

Add something like this in the created method :

        JFileChooser jfc;
        jfc = new JFileChooser();     
        File f = new File(System.getProperty("user.dir"));
        jfc.setCurrentDirectory(f);
        jfc.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
        jfc.showOpenDialog(parentFrame);
        File selFile = jfc.getSelectedFile();

Upvotes: 0

Joop Eggen
Joop Eggen

Reputation: 109557

In the Properties window there is the property fileSelectionMode to be set to DIRECTORIES_ONLY. Furthermore you can set currentDirectory, selectedFile.

BTW GUI under Swing is not at least powerful.

Upvotes: 0

Robin
Robin

Reputation: 36611

When you start with Swing, I strongly suggest to read the Swing tutorials. For example the part about the JFileChooser you want to use is located here. Those tutorials contain heaps of sample code and are a nice addition to the javadoc.

A quick scan of that tutorial did not show the answer to your question, so here the direct link to the javadoc of the method you are searching: JFileChooser#setCurrentDirectory

Upvotes: 2

Related Questions