X

Vaibhav's Blog Space

Hyperlink to URL

Guest Author

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.

Join the discussion

Comments ( 21 )
Please enter your name.Please provide a valid email address.Please enter a comment.CAPTCHA challenge response provided was incorrect. Please try again.