Add a Hyperlink to a File

Discussion in 'Java' started by Sergio, Mar 21, 2007.

  1. Sergio

    Sergio Guest

    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.
    Sergio, Mar 21, 2007
    #1
    1. Advertising

  2. On Mar 22, 10:44 am, "Sergio" <> wrote:
    ....
    > ...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.
    Andrew Thompson, Mar 22, 2007
    #2
    1. Advertising

  3. Sergio

    Sergio Guest

    On 21 mar, 19:15, "Andrew Thompson" <> wrote:
    > On Mar 22, 10:44 am, "Sergio" <> wrote:
    > ...
    >
    > > ...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.


    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
    Sergio, Mar 22, 2007
    #3
  4. Sergio

    Chris Uppal Guest

    Sergio wrote:

    > 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
    Chris Uppal, Mar 22, 2007
    #4
  5. On Mar 23, 6:35 am, "Sergio" <> wrote:
    ...
    > 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, ..
    <http://java.sun.com/j2se/1.4/pdf/versioning.pdf>
    ...and hit 'enter'.

    > 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.
    Andrew Thompson, Mar 23, 2007
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Matthew Louden

    add a hyperlink in each data field

    Matthew Louden, Nov 19, 2003, in forum: ASP .Net
    Replies:
    1
    Views:
    400
  2. =?Utf-8?B?TWljaGFlbENhZGl0eg==?=

    how can I add hyperlink to button?

    =?Utf-8?B?TWljaGFlbENhZGl0eg==?=, Feb 16, 2005, in forum: ASP .Net
    Replies:
    6
    Views:
    14,680
    Chrisl
    Nov 8, 2006
  3. davetichenor
    Replies:
    1
    Views:
    806
    Eliyahu Goldin
    Oct 30, 2006
  4. Ken
    Replies:
    1
    Views:
    362
    Alvin Bruney
    Aug 16, 2003
  5. Dave
    Replies:
    0
    Views:
    926
Loading...

Share This Page