I'd prefer it from Property editor -> Customize Code.
I need to select a folder in one instance and a file in another.
The function will be: On pressing jButton1
, jFileChooser1
opens and on confirmation of selection, returns the folder/file path selected to a String strFolderPath
/strFilePath
.
Thanks in advance for any help.

- 77
- 2
- 7
-
You add an ActionListener to the JButton and then give that ActionListener's `actionPerformed(...)` method code that opens your JFileChooser. The tutorials on both these processes will explain the individual steps on how to do this. Google can show you where to find the tutorials. More importantly your question suggests that you haven't yet gone to the tutorials, and this should be your first step since learning to use them is key to advancing in Java. Links to the Swing tutorials and other Swing resources can be found here: [Swing Info](http://stackoverflow.com/tags/swing/info) – Hovercraft Full Of Eels Aug 10 '14 at 04:00
-
1Here are more specific links for you: 1) [How to use JButtons](http://docs.oracle.com/javase/tutorial/uiswing/components/button.html), 2) [How to write an ActionListener](http://docs.oracle.com/javase/tutorial/uiswing/events/actionlistener.html), 3) [How to use a JFileChooser](http://docs.oracle.com/javase/tutorial/uiswing/components/filechooser.html). – Hovercraft Full Of Eels Aug 10 '14 at 04:05
-
1In the future, you'll get better help if you show your attempt to solve it first. If you don't do this, how will we know where you're stuck or what confuses you? Also your showing your effort gains much in terms of respect for you and your question, showing that you're willing to put in the effort and initiative to try to solve it yourself first. – Hovercraft Full Of Eels Aug 10 '14 at 04:22
-
Thanks [Hovercraft Full Of Eels](http://stackoverflow.com/users/522444/hovercraft-full-of-eels) – Arumoy Chakraborty Aug 10 '14 at 04:28
1 Answers
"I'd prefer it from Property editor -> Customize Code."
No you don't. That's editing the auo-generated code. Yo don't want/need that. The auto-generated code is just for initializing the component and laying them out. It's not meant to be altered (unless you really know what you're doing)
What you want is to add a listener to the button, and write your code for choosing a file in the listener callback. In Netbeans editor you can simply:
- Right click on the button from the design view and select Events->Action->actionPerformed
If you go to the source code view, you will see something like
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) { }
Inside that method is where out write your code for the file chooser. If you need help with that, I suuggest you take a look at How to use File Choosers. A very simple code example can also be found at the JFileChooser api javadoc
As an aside, these are pretty basic Swing use cases. I would strongly urge you to put down the GUI editor and learn to hand code first. It will make using the editor as tool much easier once you understand the code behind it. If you do want to follow this advice, keep handy the Swing tutorials and slowly go through it at your own pace. There's a lot to take in, but no on can become a ninja overnight, you need to be a grasshopper first :-)

- 205,037
- 37
- 486
- 720