Create an "accessory view" in Java Swing file dialogs - java

On Mac OS X, the native NSSavePanel supports an "accessory view" that can be used to specify file types and other options (using setAccessoryView:). I would like to do something similar in my Swing application as well.
I know JFileChooser supports something like this but it just doesn't look native. Is it possible to do this using AWT's FileDialog directly (which does use the native file dialog)? Maybe using Java Native Access?

No, it is not possible with FileDialog. Check out QuaQua. It's a Mac OS X Java look-and-feel that more closely matches the native UI. Its version of JFileChooser may be more to your liking.
If you're already familiar with the OS X API though, the best solution may be to just open a native dialog directly via JNI.

Related

Switching between JFileChooser and FileDialog

I want to use JFileChooser for Windows and FileDialog for Macs in my Java app. I have it already written in JFileChooser, do I need to completely rewrite it to handle the case for Macs (i.e check if the current OS is Mac and reimplement everything using FileDialog) or is there a easier way?
Short answer: yes, you have to reimplement everything.
Long answer: you could try to add an additional abstraction layer on top of everything, e.g. something like MyOpenDialog and MySaveDialog. These classes decide upon the OS whether to use JFileChooser or FileDialog, so you still have to implement it once. But only once, and if you need it in multiple places in your application, you can just use your own classes.
Be aware that FileDialog does not offer all the features of JFileChooser, e.g. offering the user a list of file formats to choose from (FileFilter) is only available for JFileChooser.

Check for keystroke

Im making an application that should see if your online or away.
So i need to somehow see when the user hit a key on the keyboard last time.
The application is running in the background and you can only access it from the trey.
Is this possible and if it is how would i check.
Best regards
I believe that what you're looking for can be done via a global keyboard hook, but that doesn't have direct support in the JVM. From Googling, it would appear that your only options are to write a C++ shim which you can use via JNI, or go via libffi with JRuby.
From what I've read here it is not possible to add KeyListener to the SystemTray. The only listener that is supported, is the PropertyChangedListener.
Furthermore from that question it seems to be possible on Windows but not on Linux and MacOS.
This is possible, but not using Java. You'll have to use JNI to access OS APIs that provide this information (and implent it differently for each OS).

Java FileDialog selecting directories: Mac OSX only?

I read that via
System.setProperty("apple.awt.fileDialogForDirectories", "true");
users can select directories via a FileDialog, now the FileDialog evoces the native file chooser, so that is exactly what i want but in the line above it reads: apple.awt..., does this mean this option will only work on Mac OSX?
if(no) {
great
} else {
what can i do to implement this on other operating systems than?
}
Thanks for any help!
PS: I know a lot of people suggest the use of a JFileChooser, but in this case i'd very much prefer the FileDialog, except if that's impossible
It is exactly as you feared.
AWT used native libs underneath. OSX has the feature to look for directories only, windows does not.
So youre only change is to use a dialog not based on AWT, i.e. Swing or SWT.
You can define an interface with platform specifc implementations. This gives a good looking dialog on OSX and something that works on other platforms. That's what I do.
To my knowledge, FileDialog does not support (in-code) using a directory dialog on all platforms.
You're already mentioned Swing's JFileChooser, but you may want to consider SWT for your widgets instead.
SWT is Eclipse (originally IBM)'s project to create an updated Java GUI Toolkit that still uses native widgets when they are available.
The major downside of SWT is that it is not part of the standard Java distribution... and each platform has its own SWT jar file.
Incidentally, SWT has a DirectoryDialog widget.

Mac OS X native printing from a Java desktop application

I would like to implement Mac OS X native printing in my Java desktop application.
That is, this print dialog when the user chooses to print:
which then expands to a dialog such as this:
As I understand it, native Mac OS X applications can customise the 2nd dialog by adding an option in the lower drop-down ("TextEdit" in the screenshot) which when selected displays options specific to the application.
How is the best way to go about achieving this in my Java application? I presume I will need to write some Objective C to do what I need, then use JNA/JNI to call it? Are they are pre-existing libraries that can help me?
You are correct in think you need to go down the JNI route.
Apple has a tech note on developing JNI routines here: http://developer.apple.com/library/mac/#technotes/tn2147/_index.html

How to launch the default (native) application for a given file from Java?

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+).

Categories