I know this might seem a bit odd, but i have this requirement and I want to know if this is possible.
I have a java swing application that I select a file (program) and this program is added to a list. When the list is completed, i execute the list of programs (this is like a start-up manager).
What I want to do is somehow, grab the file that I select and display it as image to my UI. For common files like pdf, doc, txt this is easy, I just have a generic image for each type. But lets say I want to execute regedit.exe or msconfig.exe, I want to be able to grab its icon (picture below) .
Does anyone know how this can be done?
Thanks
Take a look at FileSystemView.getSystemIcon(File).
It's a little limited (in that you will only get one size), but it's build in and doesn't require any additional libraries or JNA or JNI even...
File f = new File(...);
Icon icon = FileSystemView.getFileSystemView().getSystemIcon(f);
If you want the native icons you need JNI. Java has no default API for fetching the native icons in different sizes. Here are some startingpoints for windows & linux:
File icon overlay in java for windows
How do you get the icon, MIME type, and application associated with a file in the Linux Desktop?
If you do not need the exact native icons you can get the mimetype of the file and set a icon on your own:
Getting A File's Mime Type In Java
Related
Im just looking for function that can run some .exe files
But I didn't find that function in Java
ShellExecute(Operation, File, Parameters, Directory, ShowCmd : String) is there a function like that?
java.awt.Desktop provides some of that functionality:
browse can be used to open the default browser for a specific URI
open opens a file for viewing (with the default viewer of the OS)
edit opens a file for editing (with the default editor of the OS)
...
If you want to run arbitrary exe files, however, then Runtime.exec is the way to go (but using it can be tricky, as you need to consume all streams for it to not lock up).
Firstly, I am aware that my question may take some flak because JavaFX has its own FileChooser (that is largely superior/preferable to Swing's JFileChooser) and that mixing FX and Swing should be dissuaded. However - I will explain the functionality I'm trying to build and I am very open to suggestions on how else get there - if indeed I'm going in the wrong direction.
So firstly - the main application is built in JavaFX and is designed to calculate and archive parameters for biological samples. The primary observable is a tabPane wherein a new instance of a tab is generated for each new sample. As well as calculating and archive parameters for each sample, the user is also be able to store any type of data file in relation to each sample within the project. This is just for archiving - I do not plan to have in depth interaction with these files. Now, I come from a UNIX background and would very much like a sensible data structure to my "Project Folder" which can be sharable, externally editable and human-readable. In this regard, my overall idea was to make my saved output as follows:
A root folder (i.e. not a bespoke file format - just a classic folder)
Within the root folder:
1. A main XML file detailing specifics of the project
2. A second tier of folders - one for each tab of the project
Within the sample-specific folders:
1. An XML file detailing specifics of the sample
2. All the imported files/data in relation to this sample
Now - that all being said, I have the open/save functionality I'm aiming for functioning with Swing's JFileChooser. With a customised FileView class underpinning the JFileChooser, I can define a specific type of folder (both in relation to a pseudo-extension and its internal files and their content) as untraversable, give it a specific icon and then open it with the filechooser, in effect, as if it were a true file.
However, with the JavaFX Choosers, this becomes complicated. Because the ShowSaveDialog method does not exist for DirectoryChooser, from having to use FileChooser.ShowSaveDialog(), you can easily incorrectly save a folder within an already existing project (instead of rewriting it) - as, at least in OSX, this is poorly highlighted in the system file explorer. A further pain is that in selecting a folder - it does not populate the savename input box, the explorer simply descends into that directory.
Likewise - in opening a folder, the FileChooser.ShowOpenDialog() is clearly inappropriate. Using DirectoryChooser.ShowDialog() here is much more palatable, however the distinction between normal folders and project related folders (i.e. setting them untraversable and defining an icon) is something still better handled in Swing's JFileChooser.
Therefore, is anyone able to recommend how I can open a JFileChooser from within my FX app? I've tried running JFileChooser from a SwingNode, but I don't think I've implemented it correctly.
Dialog SaveDialog = new Dialog();
SaveDialog.setTitle("Save Project");
SaveDialog.setResizable(true);
SwingNode swingnode = new SwingNode();
JFileChooser SaveChooser = new JFileChooser();
SaveChooser.setAcceptAllFileFilterUsed(false);
FileNameExtensionFilter filter = new FileNameExtensionFilter(".prot projects", "*.prot");
SaveChooser.setFileFilter(filter);
SaveChooser.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
FileView view = new main.COBFileView();
SaveChooser.setFileView(view);
swingnode.setContent(SaveChooser);
SaveDialog.getDialogPane().setContent(swingnode);
Window window = SaveDialog.getDialogPane().getScene().getWindow();
window.setOnCloseRequest(event -> window.hide());
SaveDialog.showAndWait();
Or, on the other hand how I can replicate this functionality using FX?
Any help would be greatly appreciated!
I am displaying a list of files; i.e. xls, doc, pdf, odt etc., in my Java application (Eclipse RCP). When the user clicks on the file, I want to launch the appropriate (according to what the OS thinks) native application, just like it happens in Windows Explorer or the Finder.
And while I am here: It would be nice to also display the same icons that Finder or Explorer use for the different file types.
Is there a library or Eclipse plugin for this?
What you want is java.awt.Desktop:
Desktop.getDesktop().open( file );
I have found an API in Eclipse's SWT now that seems to do the trick:
org.eclipse.swt.program.Program "provides access to facilities for discovering operating system specific aspects of external program launching."
It has methods to find the program for a given file extension, get the program's icon, and even launch the program.
Sounds like you're after the Java Activation Framework ("JAF"). This API lets you determine what files are and what actions you can do on them. Or alternatively the Java Desktop Integration Component ("JDIC"). JDIC allows you to create and no doubt query file associations.
Both projects seem to be in a semi-abandoned state howeer (sigh). But that's par for the course for Sun these days. Only other thing I know of is some Windows specific third party library that's based on JNI called Winpack. It does a bunch of other things too.
You can get the associated icon using the FileSystemView class (Java 1.4+).
I am trying to create a program were the user drags a file into an area (currently a JTextArea, but can be another container) and it adds the absolute path of the file to an ArrayList. I am having trouble figuring out how to implement drag and drop of files.
so far I have tried reading some similar questions but they aren't really helping me.
(also this is targeting windows but linux/mac support is an option as well)
Have a look at oracles page about DnD. Basically you can drag everything into your program, should it be a file directly from a native browser or the JFileChooser. What you are dragging is only the path to the file. So you only have to set your JTextAreato accept drops and define how it has to "interpret" the object that was being dropped.
Here is a full blown example.
There are plenty of tutorials out there on how to copy files to the clipboard using the SWT API. However, I've never seen an explanation of how to cut files to the clipboard. What I'm trying to do is write a simple file manager in Java where you can select a file and press Ctrl+X, and when you press Ctrl+V in the native file manager, the files are moved (not copied) from their original location to the destination. Does the SWT API actually support this, or is some lower-level API required to get this done?
Check out this image sample. Note that there is a FileTransfer class instead of ImageTranfer class which you can use instead.
Concerning CUT operation:
To do this, you must manually erase the data once it is copied to the clipboard. Java provides no implementation of a cut operation.. This is with AWT/Swing but I am pretty sure it's the same thing with SWT. You can remove it after successful clipboard copy or successful paste...your choice.