2

I have used JNA library and this small API (JnaFileChooser) https://github.com/steos/jnafilechooser

JnaFileChooser fc = new JnaFileChooser();
fc.addFilter("All Files", "*");
fc.addFilter("Pictures", "jpg", "jpeg", "png", "gif", "bmp");
if (fc.showDialog(parent)) {
File f = fc.getSelectedFile();
// do something with f

}

But how do I use JNA to access this dialg "select folder"

enter image description here

d.civ
  • 31
  • 2

1 Answers1

0

The entire dialogue is controlled on the native side. The package you are are using is already accessing that dialog and that button.

Tracing through the source code of the JnaFileChooser class, this dialogue is part of the WindowsFolderBrowser class. The dialog appears using the SHBrowseForFolder() function combined with SHGetPathFromIDList, and returns the path when the Select Folder button is pressed.

final Pointer pidl = Shell32.SHBrowseForFolder(params);
if (pidl != null) 
    // MAX_PATH is 260 on Windows XP x32 so 4kB should
    // be more than big enough
    final Pointer path = new Memory(1024 * 4);
    Shell32.SHGetPathFromIDListW(pidl, path);
    final String filePath = path.getWideString(0);
    final File file = new File(filePath);
    Ole32.CoTaskMemFree(pidl);
    return file;
}

The params variable passed to this function is of native type BROWSEINFO which controls the dialog box. You can see in the code how a few things have been assigned to it (abbreviated version of code):

final Shell32.BrowseInfo params = new Shell32.BrowseInfo();
params.hwndOwner = Native.getWindowPointer(parent);
params.ulFlags = Shell32.BIF_RETURNONLYFSDIRS | Shell32.BIF_USENEWUI;
params.lpszTitle = title;

If you want to change anything else about the dialog, you need to use a callback. One of the elements in BROWSEINFO is BFFCALLBACK lpfn; where you would define that function, e.g., params.lpfn = the defined callback function.

Documentation for BFFCALLBACK indicates you'll use the option to use SendMessage to change the OK button text with BFFM_SETOKTEXT.

Daniel Widdis
  • 8,424
  • 13
  • 41
  • 63