get selected file path in windows explorer with Java - java

I'm developping a Java application in which the user is able to change the background of a JFrame using a picture from his computer.
In order to do that I'm trying to get the path name of the picture he selects in windows explorer.
I use this code to open the explorer but I can't figure out what I got to do next to get the selected file path...
p = new ProcessBuilder("explorer.exe", "/select,C:\\directory\\selectedFile").start();
Any ideas ?
Thanks.

You should use a JFileChooser instead.
Then retrieve the selected file as a File object using the getSelectedFile() method.

Better option is JFileChooser for selecting a file.

I would suggest using JFileChooser.

That may be possible, but it is much easier to use a JFileChooser. You can set the look and feel to mimic the current environment (Windows, in your case). I recommend googling JFileChooser as there are tons of examples (including in the API).

Related

Getting System Icon Without Path [duplicate]

I list some filenames with their icons (like the ones in the Windows Explorer) in a JTable. I know the two ways to get the icon if I have a File object from the local file system:
javax.swing.filechooser.FileSystemView.getFileSystemView().getSystemIcon( file )
for a 16x16 icon or for a bigger one:
sun.awt.shell.ShellFolder.getShellFolder( file ).getIcon( true ) )
Since my files are stored in a database, I don't have the File object. My workaround is to create a temp file with the specific filename extension, use one of the two methods above and cache the icon to display it in a CellRenderer.
I searched for a solution without temporary files and found two I don't like either:
org.eclipse.swt.program.Program.findProgram(String extension).getImageData(), but I don't want to use SWT
org.jdesktop.jdic.icons.IconService from the Incubator of the JDIC project. The last changes on the IconService are 6 years ago, on JDIC 2 years ago and I can't find a downloadable jar.
Is there another solution?
Looks like you already discovered the way to do it, unless you want to dive into native libraries etc.
FileSystemView uses Win32ShellFolder internally so they are basically the same.
I also dug up the Source for org.eclipse.swt.program.Program and with it org.eclipse.swt.internal.win32.OS. The OS class then uses a native call for the Icon. At this point unless you really really cannot create a Temp File i would not go down that path.
For JDIC i only found http://kickjava.com/src/org/jdesktop/jdic/tray/internal/impl/WinTrayIconService.java.htm with a little bit of digging(may not be related but does icony things :D). Also calls native.
Do you really need the temporary file to use the first option? A File does not have to denote a file that actually exists...

Get Windows files associations via Java

I thought this would be an easy task but . . .
I want my Java program to get the user's Windows file associations.
In other words, I want to know what the user uses to open .txt files, .cvs files, etc.
The assoc and ftype commands provide that info, but not for the user.
In other words, if I've set my text editor to Notepad++,
assoc and ftype don't show it. They show the system default, Notepad, instead.
It looks like I have to get that info from the registry but I have two problems.
1) I don't know the exact registry keys I want to pull
(though I've looked at "reg query HKEY_CURRENT_USER, HKEY_CLASSES_ROOT, etc.)
2) I don't know how to pull the key from the registry. I've seen JNI mentioned
but haven't figured out the details.
Any hints appreciated.
In Win7, you can find the "class"* for each file extension in the
HKLM\SOFTWARE\Classes\<extension>\(Default)
key.
For example, on my machine
HKLM\SOFTWARE\Classes\.txt
has a (Default) key of txtfile.
In that same path, you can find what opens files of the class txtfile:
HKLM\SOFTWARE\Classes\txtfile
Which should have a subpath of ...\shell\open\command
On my system,
HKLM\SOFTWARE\Classes\txtfile\shell\open\command\(Default)
is
%SystemRoot%\system32\NOTEPAD.EXE %1
Which you could parse to find the executable that opens .txt files.
For user-specific customizations, you can replace
HKLM\SOFTWARE\Classes\
with
HKCU\Software\Classes\
All that being said, I like MadProgrammer's suggestion if possible for your application.
* I'm sure there is a better name for "class", I just don't know it.
Take a look at the Eclipse's Program class, which should to what you want: http://help.eclipse.org/helios/index.jsp?topic=%2Forg.eclipse.platform.doc.isv%2Freference%2Fapi%2Forg%2Feclipse%2Fswt%2Fprogram%2FProgram.html.
If you would like to launch the program, use the Desktop-class (like MadProgrammer suggested)

How to get the icon for a file extension or filetype without creating a temp file?

I list some filenames with their icons (like the ones in the Windows Explorer) in a JTable. I know the two ways to get the icon if I have a File object from the local file system:
javax.swing.filechooser.FileSystemView.getFileSystemView().getSystemIcon( file )
for a 16x16 icon or for a bigger one:
sun.awt.shell.ShellFolder.getShellFolder( file ).getIcon( true ) )
Since my files are stored in a database, I don't have the File object. My workaround is to create a temp file with the specific filename extension, use one of the two methods above and cache the icon to display it in a CellRenderer.
I searched for a solution without temporary files and found two I don't like either:
org.eclipse.swt.program.Program.findProgram(String extension).getImageData(), but I don't want to use SWT
org.jdesktop.jdic.icons.IconService from the Incubator of the JDIC project. The last changes on the IconService are 6 years ago, on JDIC 2 years ago and I can't find a downloadable jar.
Is there another solution?
Looks like you already discovered the way to do it, unless you want to dive into native libraries etc.
FileSystemView uses Win32ShellFolder internally so they are basically the same.
I also dug up the Source for org.eclipse.swt.program.Program and with it org.eclipse.swt.internal.win32.OS. The OS class then uses a native call for the Icon. At this point unless you really really cannot create a Temp File i would not go down that path.
For JDIC i only found http://kickjava.com/src/org/jdesktop/jdic/tray/internal/impl/WinTrayIconService.java.htm with a little bit of digging(may not be related but does icony things :D). Also calls native.
Do you really need the temporary file to use the first option? A File does not have to denote a file that actually exists...

opening file with java application automatically

I want to create my own file format for a particular kind of file. When someone downloads this file I want their system to know it should be opened with my application.
For example when I download a .doc file, my computer asks me whether I want to save the file or open it with Open Office. Similarly, If that .doc file is sitting on my desktop, and I double click it, it automatically opens with the correct application.
I believe this has to do with associating the file extension with the application in the context of the underlying OS.
Can any one point me to some good resources about how to do this in java?
Thanks.
Edit:
Sorry I want to clarify. Is there a way I can have my application associate the file type with itself when it is installed?
Edit:
found this...
http://java.sun.com/developer/technicalArticles/J2SE/Desktop/jdic_assoc/
platform independent solution
This source shows how to make a file association in windows:
http://www.rgagnon.com/javadetails/java-0592.html
You will probably have to do it per installer that you make in each OS.
found this... http://java.sun.com/developer/technicalArticles/J2SE/Desktop/jdic_assoc/ platform independent solution

Opening file from Java

What I need is to instruct OS to open the file with the default program used for that file type. Exactly as if, i.e., that file was double-clicked by user under Windows.
The purpose is, i.e., "your PDF file was generated. Click here to open it".
In platform-independent way, if possible...
I dont know exact terms for what I want, so if someone could update tags, I'd most appreciate that ;)
You need the Desktop class, and the open() method in particular.
Launches the associated application to
open the file. If the specified file
is a directory, the file manager of
the current platform is launched to
open it.
Since Java 6, we have Desktop.open() for exactly that purpose.

Categories