• Post Reply Bookmark Topic Watch Topic
  • New Topic

Need help c JList & File opening

 
bram simiyu
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have several a JLists from which I've listed files on my drive. I wish to be able to open/run the files(eg word documents/excel/pdf) when the file of choice is double clicked. I want the file to be opened to be based on the JList choice. Please help!
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ah, this doesnt seem to have anything to do with Servlets, the forum you posted in, so I'm moving it to Swing where people may be able to help...
 
Gregg Bolinger
Ranch Hand
Posts: 15304
6
Chrome IntelliJ IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, basically, when you click on the item, you need to find the extension of the file, then use some SWITCH CASE or IF statement to determine which program to use. Then you will need to use Runtime.exec(args) to run the appropriate application. Most applications like Word and Excel will allow you to run a file and open the document like:
"excel.exe filename.xls"
That is the only way I can think of. Since application and file extension mapping is very OS specific, I can't see something like this being inherent in the JAVA API anywhere.
 
Nathan Pruett
Bartender
Posts: 4121
IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
On windows, with some files you can pass them to the cmd.exe (or whatever is appropriate for your windows version) and it will launch the file in the associated application.

Also, if you're not tied to Swing, I seem to remember SWT does provide some kind of "launch this file in the associated program" code that at some level touches the OS (if you don't define an association yourself)... if you're interested look at the org.eclipse.swt.program.Program class.
 
Gregg Bolinger
Ranch Hand
Posts: 15304
6
Chrome IntelliJ IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Also, if you're not tied to Swing, I seem to remember SWT does provide some kind of "launch this file in the associated program" code that at some level touches the OS (if you don't define an association yourself)... if you're interested look at the org.eclipse.swt.program.Program class.
Ahh, I forgot about that. SWT also offers some ActiveX libraries and lets you actually embed certain Windows Apps like Excel and Word inside of an SWT Frame/Application. Might serve your purpose a bit.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!