Prasanna
Prasanna

Reputation: 305

JFileChooser - browsing to shared path as \\machineIp

I am using JFileChooser in my Swing application. The user needs to browse to shared path such as \\ip\ (e.g. \\100.150.200.222\ -- in windows explorer this path is accessible and contains all shared folders on that machine). When the user types \\ip\ and clicks "OPEN" the file chooser window closes. Instead if the user gives any shared folder name in the remote system, then the file chooser displays the files (e.g. \\100.150.200.222\sharedFolder\). Kindly help on overcoming this so that the user can browse to \\ip\

Upvotes: 1

Views: 892

Answers (1)

Eric Leibenguth
Eric Leibenguth

Reputation: 4277

\\100.150.200.222\ -- in windows explorer this path is accessible and contains all shared folders on that machine

Indeed, but \\100.150.200.222 is not a folder itself, which is why the file chooser does not work. In order to list the shared folders at this IP address, the solution is to use a 3rd party library as suggested in this post: Shares Under IP

String[] foldernames = new SmbFile("smb://100.150.200.222/").list();

Then I see three possible solutions to let the user choose a file:

  1. You know that there's only one relevant shared folder at this IP address:

Then, that's very simple: Get the name of the 1st shared folder with JCIFS, and open it in your file chooser.

File folder = new File("\\100.150.200.222\"+foldernames[0]);
new JFileChooser(folder).showSaveDialog(null);
  1. Let the user choose the shared folder with an intermediate dialog. This is not great from the user experience point of view, but it can be done in one line with JOptionPane.showInputDialog().

String foldername = JOptionPane.showInputDialog(..., foldernames, ...);

  1. You really need the list of shared folders to appear in the file chooser. This is not easy, but is not impossible. Have a look at this post. The challenge is that you have to extend File and override a number of relevant methods. Then feed this file to your file chooser.

Upvotes: 2

Related Questions