2

I making an app in Java. I want an Open With Dialog to appear when a JButton is clicked.

I know that in Windows, the contents of the Recommended Programs list are determined from the registry entry

HKCU\Software\Microsoft\Windows\CurrentVersion\Explorer\FileExts\filetype\OpenWithList

..where 'filetype' is the required file type.

Is there any way that this can be done with complete independence of the OS in which this app runs? Or do I have to get the OS name and program accordingly?

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
Sandy
  • 23
  • 4

2 Answers2

4

Not completely sure whether this answer will be relevant as I am making some assumptions. In case it is a file you want to open, you can use Desktop#open which will open the File with the default application.

I assume (but did not test) that when no default application is set, a dialog will be shown asking what application you want to use to open that file (similar as when you double click such a file in your file browser).

This is not completely what you asked for, but might be sufficient.

Otherwise I am afraid I agree with Andrew's answer that there is no general way to do this.

Robin
  • 36,233
  • 5
  • 47
  • 99
  • *"I assume (but did not test)"* I did not know, was curious, so tested it to see.. `Exception in thread "main" java.io.IOException: Failed to open file:///I:/.../preface.zz1. Error message: Unspecified error`. No friendly dialog or menu of options, unfortunately. +1 for guessing the OP did not want what they said! I ruled out `Desktop` since it never presents a choice! ;) – Andrew Thompson Aug 22 '12 at 14:12
  • @AndrewThompson Thanks for the information. So that means the JDK has another behavior then the double-click in your file browser, on your desktop ... what a bummer – Robin Aug 22 '12 at 14:37
  • 1
    Got it in one. If I double click the `.zz1` extension file (a renamed HTML), I see the usual Windows "Don't know what to do with this - look for app./consult the web service" dialog. And yes, that is a bummer. It would be the logical thing to do in that case, only throwing an exception if the user 'escapes' the dialog. ..I just realized that the JRE might not be able to detect what happens then, and that is why it simply bombs out. – Andrew Thompson Aug 22 '12 at 14:43
  • _JRE might not be able to detect what happens then_ - good guess: I faintly remember a discussion with a former swing team member (around using the Desktop.browse method) with exactly that outcome :-) – kleopatra Aug 22 '12 at 15:40
0

Is there any way that this can be done with complete independence of the OS in which this app runs?

No.

Or do I have to get the OS name and program accordingly?

Yes.

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433