Add a Hyperlink to a File

S

Sergio

Hi everyone : )

I want to create an interactive CD for a text database with Java, the
text files are in pdf format and I was wondering.. Is it possible to
create a hyperlink so that when the user clicks the link the pdf
opens? If it isn't possible with an applet maybe with a jar
application?

I have been searching the net but to no avail... so... I tough that
asking the developer comunity would be a good idea.

I'm not interested in making this feature functional for all systems,
windows is more than enough since the intended audience uses that
system.

Thanks in advance.

Sergio.
 
A

Andrew Thompson

...Is it possible to
create a hyperlink so that when the user clicks the link the pdf
opens?

'Sure'. I put that in inverted commas, because
you can also have buttons, menus or other GUI
elements that will act the same as a link, but
if the app. requires something that also *looks*
like a link, then that is also doable.

JEditorPane can render standard (simple) HTML 3.2,
and in certain conditions (see the API Docs) links
become 'actionable' (HyperLinkListener).

Failing that, something like a standard label can
be overridden to act like a link, with a MouseListener
to detect when it should take the 'hover' color,
and also when it is clicked.
..If it isn't possible with an applet maybe with a jar
application?

Note that applets can be either Jar'd or loose
classes, just like an application - but you (and
the end user) are almost always better off using
Jar'd classes.

To open a PDF from within an applet might be
as simple as 'showDocument(URLofPDF);' but note
that the applet showDocument() is, and always
will be, horridly unreliable.

I feel you are better off looking to either
a) open the PDF using Java functionality, or
b) use web start's equivalent 'showDocument'*
method to toss it at the default browser (the
show document method in web start reports
success or failure - though I am not aware of
any systems on which it fails).

* e.g. <http://www.physci.org/jws/#bs>

HTH

Andrew T.
 
S

Sergio

'Sure'. I put that in inverted commas, because
you can also have buttons, menus or other GUI
elements that will act the same as a link, but
if the app. requires something that also *looks*
like a link, then that is also doable.

JEditorPane can render standard (simple) HTML 3.2,
and in certain conditions (see the API Docs) links
become 'actionable' (HyperLinkListener).

Failing that, something like a standard label can
be overridden to act like a link, with a MouseListener
to detect when it should take the 'hover' color,
and also when it is clicked.


Note that applets can be either Jar'd or loose
classes, just like an application - but you (and
the end user) are almost always better off using
Jar'd classes.

To open a PDF from within an applet might be
as simple as 'showDocument(URLofPDF);' but note
that the applet showDocument() is, and always
will be, horridly unreliable.

I feel you are better off looking to either
a) open the PDF using Java functionality, or
b) use web start's equivalent 'showDocument'*
method to toss it at the default browser (the
show document method in web start reports
success or failure - though I am not aware of
any systems on which it fails).

* e.g. <http://www.physci.org/jws/#bs>

HTH

Andrew T.

Thanks for your answer Mr. Thompson, I really appreciate it.

I tried what you suggested, first the hyperlink listener, the problem
is that you have to add code in the hyperlink for it to work, I
mean... the hyperlink listener kinda rises an event, so I still need
to add code for the hyperlink to open the PDF and I don't know how to
tell the host system to open the PDF.

Well, the showDocument() function works but only with web pages and
even if it worked with files the function needs the entire URL and I
canot foretell the drive of the CD drive the user will have or even if
he'll be running the program from the CD. Have been peeking in other
applications similar to the want I want to design but looks like no
buddy have ever made something like this.

If you happen to come across with the solution to how to tell the host
system to open a file and you can lend it to me I will be very
thankful. You can contact me via e-mail if you would prefer.

Again Thanks for your time.

Sincerely:

Sergio
 
C

Chris Uppal

Sergio said:
If you happen to come across with the solution to how to tell the host
system to open a file and you can lend it to me I will be very
thankful.

You may be able to create a Windows-only solution using the Windows command:

rundll32 url.dll,FileProtocolHandler <some filename or URL>

which, I believe, opens the indicated file with the user's default program for
opening that file-type, or opens the user's default browser on the given URL.
Other URLs schemes (mailto: etc) should also work if Windows understands the
scheme well enough to know what application to launch.

It seems to work for me with both absolute and relative filenames (UNC
filenames too, which surprised me).

I don't really know how it works, so I can't comment on how widely available it
is (on Windows), or on how reliable it is, or on what hidden gotcha's there may
be. If the idea seems useful at all then I suggest some research with Google
and at MSDN.

-- chris
 
A

Andrew Thompson

I tried what you suggested, first the hyperlink listener, the problem
is that you have to add code in the hyperlink for it to work,

Oh, if I'd known you did not want to do any
coding, I'd have recommended elance.
..I
mean... the hyperlink listener kinda rises an event, so I still need
to add code for the hyperlink to open the PDF and I don't know how to
tell the host system to open the PDF.

Try this link..
<http://www.physci.org/jws/basicserv.jnlp>
...it should open a small app. on-screen.
Then paste this URL into the JTextField, ..
Well, the showDocument() function works but only with web pages

Did you confirm that locally? Where is your code?
What is your system description? How did it fail?

...or are you just making this up as you go along?
..and
even if it worked with files the function needs the entire URL and I
canot foretell the drive of the CD drive the user will have or even if
he'll be running the program from the CD.

Can you get a File handle to the PDF of interest?
I understood that you could. If that is the case,
converting it to URL is simple, if not, you need
to solve that first.
..Have been peeking in other
applications similar to the want I want to design but looks like no
buddy have ever made something like this.

It is quite common and aesy.
If you happen to come across with the solution to how to tell the host
system to open a file and you can lend it to me I will be very
thankful.

Most of the solution has been laid out for you
in my first post. If you had taken the time to
try some of the things to which I linked.
...You can contact me via e-mail if you would prefer.

You can pay me consultancy rates, if you prefer,
or we can continue this here, for free. But here,
I expect you to read my replies carefully, and try
the examples that I link to.

As an aside, you will get better help from me if you
turn your "I can't do that" moaning into "How do I do
that?".

Andrew T.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top