Vaibhav's Blog Space

Hyperlink to URL

Sunday Jul 26, 2009

It's a long time being blogging. Actually not done anything new from long time :). Here is one simple concept which some guys asked me. We have provided hyperlink API in JavaFX 1.2 but some of us struggled to open a URL using hyperlink API.

Hmm, 2 ways to do it actually.

 No1 : Use the Desktop API of JDK6. It's simple to use. One example is here.

So, very basic code will go like this :

  

package sample2; import javafx.scene.Scene; import javafx.stage.Stage; import javafx.scene.control.*; import java.net.*; Stage {     title: "HyperLink to URL"     width: 240     height: 320     scene: Scene {         content: [             Hyperlink{                 translateY: 160                 translateX: 40                 width: 150                 text: bind "Visit javafx Samples! "                 action: function():Void{                     java.awt.Desktop.getDesktop().browse(new URI("http://javafx.com/samples"));                 }             }         ]     } }

So, 2 things for running this code. First,Desktop API has been added in JDK6, so this code won't run on JDK5. Second, Add rt.jar(rt.jar of JDK6) file in the Libraries if you are using Netbeans

No2 : For only JavaFX code, we can use AppletStageExtension like this :

package sample1;

import javafx.stage.Stage; import javafx.scene.Scene; import javafx.scene.control.Hyperlink; import javafx.stage.AppletStageExtension; Stage {     title: "Hyperlink to URL"     width: 250     height: 80     scene: Scene {         content: [             Hyperlink {                 text: "JavaFX Samples !"                 action: function() {                        AppletStageExtension.showDocument("http://javafx.com/samples");                 }             }         ]     } }


In this case, you cant send hyperlink from Desktop Application, but it will work fine for applet or Browser application. So, best is to use this and then use our normal funda : if {__PROFILE__}" != "browser") --> use the Desktop API code. What you say :).

Please let me know if there is any issue in the code ! Or also if there is any better way to do this.

[11] Comments
Like this post? del.icio.us | furl | slashdot | technorati | digg
Comments:

Nice article :O) , rather on *nx machine i was using sed with an RE to get the URL.

Posted by hemanth.hm on July 26, 2009 at 04:20 PM IST #

:D Thank you. your is complicated one :)

Posted by Vaibhav on July 26, 2009 at 07:25 PM IST #

It seems that java.awt.Desktop.getDesktop() method will not work on Mac with JDK 1.5.

I have created the issue RT-5222 to have a unified way for a URL opening in a browser.

http://javafx-jira.kenai.com/browse/RT-5222

Posted by Alexandr on July 27, 2009 at 02:38 PM IST #

Nice. I guess in place of bug you can move it into feature request.
Because this apparently need to go down to JDK not on JavaFX. Underlying JDK dont provide any such feature in 1.5. We need to move on JDK6 :D.

Posted by Vaibhav Choudhary on July 27, 2009 at 02:49 PM IST #

Good tip. Also you can try the more complex but flexible code at
http://www.centerkey.com/java/browser/

Posted by Winston Prakash on July 29, 2009 at 01:44 AM IST #

ah i see. yes, this also sounds good :). Thanks for sharing !

Posted by Vaibhav on July 29, 2009 at 08:29 PM IST #

Vaibhav,

Thanks for posting this. Per your suggestion, I had to add %JAVA_HOME%/Java/jre6/lib/rt.jar to make this work. "rt.jar" is already present in the Java platform and is visible in Netbeans via Tools --> Java Platforms. So, why does one have to add "rt.jar" to the libraries of this particular project again ?.

/rk

Posted by rk tumuluri on September 13, 2009 at 09:59 AM IST #

I found your post really interesting. I found Internet marketing a really a great way of promoting a website. I found good results when I started internet marketing my website.

Posted by Jeff Paul Shortcuts on October 06, 2009 at 01:01 AM IST #

thank you

Posted by Vaibhav Choudhary on October 06, 2009 at 10:40 AM IST #

Really Nice Post. More and more businesses are indulge with online presence, it is costly if you find a wrong company to do your job

Posted by Internet Business Review on October 11, 2009 at 01:08 AM IST #

Its very intresting and Informative stuff. Women finds it difficult to manage their cost, save $1 a day and you can see the difference it makes

Posted by Wealthy Women Debts on November 20, 2009 at 02:27 AM IST #

Post a Comment:
  • HTML Syntax: NOT allowed