Original page address: Http://docs.oracle.com/javafx/2/ui_controls/hyperlink.htm#CIHGADBG
This chapter describes the components used to convert text to hyperlinks Hyperlink
Hyperlink
A class is another form of the labeled class.
Figure 18-1 shows the 3 state of the default hyperlink implementation
Figure 18-1 The 3 state of the hyperlink component
Create a hyperlink
Example 18-1 shows the code snippet that created the hyperlink
<em> example 18-1 Typical hyperlink </em>hyperlink link = new Hyperlink (); Link.settext ("http://example.com"); Link.setonaction (New eventhandler<actionevent> () { @Override public void handle (ActionEvent e) { SYSTEM.OUT.PRINTLN ("This link is clicked"); });
settext
Member method to define the title of the hyperlink.
because Hyperlink class is an extension of the labeled class, you can set a specific font and text for the title.
< Span style= "Color:rgb (51,51,51); Font-family:verdana,arial,helvetica,sans-serif; Font-weight:bold; line-height:19.2000007629395px "> setonaction
method is used to specify the behavior of hyperlinks when clicked. Similar to The onaction action of the button.
Example 18-1, in which behavior is limited to printing strings. But in your app, you might want to use it for more common tasks.
Link Local content
Render a picture from a local directory in the show app in Figure 18-2
Figure 18-2 Displaying the picture
Show Example Source code for 18-2 :
Example 18-2 Browse pictures with hyperlinks
Import Javafx.application.application;import Javafx.event.actionevent;import Javafx.event.eventhandler;import Javafx.scene.*;import Javafx.scene.control.*;import Javafx.scene.image.image;import Javafx.scene.image.ImageView; Import Javafx.scene.layout.vbox;import javafx.stage.Stage; public class Main extends application {final static string[] imagefiles = new string[]{"Product.png", "Education.png", "Partners.png", "Support.png"}; Final static string[] captions = new string[]{"Products", "Education", "Partners", "support" }; Final ImageView selectedimage = new ImageView (); Final ScrollPane list = new ScrollPane (); Final hyperlink[] Hpls = new Hyperlink[captions.length]; Final image[] images = new Image[imagefiles.length]; public static void Main (string[] args) {application.launch (args); } @Override public void start (stage stage) {Scene scene = new Scene (new Group ());Stage.settitle ("Hyperlink Sample"); Stage.setwidth (300); Stage.setheight (200); SELECTEDIMAGE.SETLAYOUTX (100); Selectedimage.setlayouty (10); for (int i = 0; i < captions.length; i++) {final Hyperlink HPL = hpls[i] = new Hyperlink (captions[i]); Final image image = Images[i] = new Image (GetClass (). getResourceAsStream (Imagefiles[i]) ); Hpl.setonaction (New eventhandler<actionevent> () {@Override public void handle (Actione Vent e) {selectedimage.setimage (image); } }); Final button button = New button ("Refresh links"); Button.setonaction (New eventhandler<actionevent> () {@Override public void handle (ACTI OnEvent e) {for (int i = 0; i < captions.length; i++) {hpls[i].setvisited (f Alse); SeLectedimage.setimage (NULL); } } }); VBox VBox = new VBox (); Vbox.getchildren (). AddAll (HPLS); Vbox.getchildren (). Add (button); Vbox.setspacing (5); (Group) Scene.getroot ()). GetChildren (). AddAll (VBox, selectedimage); Stage.setscene (Scene); Stage.show (); }}<em></em>
The program creates 4 Hyperlink
through a for loop, and the user clicks on a link that will trigger this hyperlink setOnAction
方法里定义的行为的动作。
so the corresponding picture in the image array is set to selectedImage
variable. When the user clicks on a hyperlink, the hyperlink appears to be visited. You can setVisited
Refresh the hyperlink by calling the method.
Example 18-3 code snippet that implements the task.
Example 18-3 Refresh Hyperlinks
Final button button = New button ("Refresh links"); Button.setonaction (new eventhandler<actionevent> () { @ Override public void handle (ActionEvent e) {for (int i = 0; i < captions.length; i++) { hpls[i].setvisited (false); Selectedimage.setimage (null);}} );
is 18-3 shown, when clicked when the button is refreshed, all hyperlinks are set to an inaccessible state.
Figure 18-3 Hyperlinks that have not been visited
because Hyperlink
class expands from Labeled
class, you can not only specify the caption text but also set a picture for it. The next section of the program will show both using captions and using pictures to create hyperlinks and load remote HTML pages.
Link Remote content
javafx program, by embedding webview
browser component to render HTML content. webview
&NBSP; component provides basic functionality for browsing Web pages. The component can render Web pages that support user and link interaction, or you can execute javascript code.
Learning Example 18-4 the source code. It created 4 hyperlinks with captions and pictures. When you click on one of the hyperlinks, the corresponding value is passed to the Mosaicked browser as a URL .
Example 18-4 loading Remote Web pages
Import Javafx.application.application;import Javafx.event.actionevent;import Javafx.event.eventhandler;import Javafx.scene.*;import Javafx.scene.control.*;import Javafx.scene.image.image;import Javafx.scene.image.ImageView; Import Javafx.scene.layout.hbox;import Javafx.scene.layout.priority;import Javafx.scene.layout.vbox;import Javafx.scene.text.font;import Javafx.scene.web.webengine;import Javafx.scene.web.webview;import Javafx.stage.Stage; public class Main extends application {final static string[] imagefiles = new string[]{"Product.png", "Education.png", "Partners.png", "Support.png"}; Final static string[] captions = new string[]{"Products", "Education", "Partners", "support" }; Final static string[] urls = new string[]{"http://www.oracle.com/us/products/index.html", "http://education . oracle.com/"," http://www.oracle.com/partners/index.html "," http://www.oracle.com/us/support/Index.html "}; Final ImageView selectedimage = new ImageView (); Final hyperlink[] Hpls = new Hyperlink[captions.length]; Final image[] images = new Image[imagefiles.length]; public static void Main (string[] args) {launch (args); } @Override public void start (stage stage) {VBox VBox = new VBox (); Scene scene = new Scene (VBox); Stage.settitle ("Hyperlink Sample"); Stage.setwidth (570); Stage.setheight (550); SELECTEDIMAGE.SETLAYOUTX (100); Selectedimage.setlayouty (10); Final WebView browser = new WebView (); Final Webengine webengine = Browser.getengine (); for (int i = 0; i < captions.length; i++) {final Hyperlink HPL = hpls[i] = new Hyperlink (captions[i]); Final image image = Images[i] = new Image (GetClass (). getResourceAsStream (Imagefiles[i])); Hpl.setgraphic (new ImageView (image)); Hpl.setfont (Font.font ("ARial ", 14)); Final String url = urls[i]; Hpl.setonaction (New eventhandler<actionevent> () {@Override public void handle (Actione Vent e) {webengine.load (URL); } }); } HBox HBox = new HBox (); Hbox.getchildren (). AddAll (HPLS); Vbox.getchildren (). AddAll (Hbox, browser); Vbox.setvgrow (browser, priority.always); Stage.setscene (Scene); Stage.show (); }}
similar examples 18-2 hyperlink through for loop to create. Sets the behavior of the hyperlink to the URL array corresponding to the URLs that are nested in the browser WebEngine
object .
when the program is compiled and run, the program form displays 18-4 the situation.
Figure 18-4 from Oracle company URL Loading page
Using JavaFX UI Controls 18 Hyperlinks