1

I am a beginner in Java, so my question would be pretty basic. Here is my problem I have a class where I process folders for query matching and indexing.I have made a GUI for the same where I will ask the user to select the directory from JFileChooser.

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                         


   JFileChooser f=new JFileChooser();
   f.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
   f.showOpenDialog(null);
   if(f.showOpenDialog(this)==JFileChooser.APPROVE_OPTION){

}
    System.out.println(f.getSelectedFile().getAbsolutePath());


}         

Now I want to give this directory path to my other class so it can process the folder

 obj = new object();
            boolean index = false;
            String str = "";
            InputStreamReader r = new InputStreamReader(System.in);
            obj.dataDirectory = // here should be the path of the selected folder

For Example, if a user selects c:\Desktop\TextFiles then what I want want is obj.dataDirectory=c:\Desktop\TextFiles

I tried creating an object of GUI class in this class and then tried calling that method in this class but I don`t know how to do it exactly or even if it is possible.

Thanks in Advance

WiLL_K
  • 575
  • 1
  • 3
  • 22
  • This stuff is right in the at the very top of [the documentation.](https://docs.oracle.com/javase/8/docs/api/javax/swing/JFileChooser.html) `getSelectedFile()` – markspace Dec 06 '16 at 23:24
  • I know about getSelectedFile() but I want obj.dataDirectory to have that path. – WiLL_K Dec 06 '16 at 23:27
  • 1
    Use `getSelectedFile()` in your second snippet of code you posted. This will require you to make `JFileChooser f` have a slightly larger variable scope so you can reference where ever that other code is. – NESPowerGlove Dec 06 '16 at 23:33
  • Thanks NESPowerGlove Solved it. – WiLL_K Dec 06 '16 at 23:45
  • See also this related [example](http://stackoverflow.com/a/10067256/230513). – trashgod Dec 07 '16 at 06:04

0 Answers0