Mithun's Memoirs

« JavaFX Menus | Main | popuri.us »
Tuesday Dec 11, 2007

Hyperlinks JavaFX

In my previous example, we used a Button to handle the mouse clicks and increment the variable that is displayed on the text label. We could also use hyperlinks to do the same. We do do a lot more with the hyperlinks such as opening a webpage and so on, but we shall retain the previous example for consistency. Here, I shall be using the hyperlinks to increment the value as I mentioned it earlier. The source listing is as follows:

 /*
 * Main.fx
 *
 * Created on Dec 11, 2007, 10:52:43 AM
 */

package text;

/**
 * @author mithun
 */

import javafx.ui.*;
 
        class ButtonClickModel {
            attribute numClicks: Number;
        }

        var model = new ButtonClickModel();

        Frame {
            width: 200
            height:200
            content: GridPanel {
                border: EmptyBorder {
                   top: 30
                   left: 30
                   bottom: 30
                   right: 30
                }
                rows: 2
                columns: 1
                vgap: 10
                cells:
                [Label {
                     text: bind
                             "<html>
                                <a href='{#(operation() {model.numClicks++;})}'>
                                    Click
                                </a>
                             </html>"
                },
                Label {
                    text: bind "Count: {model.numClicks}"
                }]

            }
            visible: true
        };

 

From the code snippet, you could observe the following:

[Label {
                     text: bind
                             "<html>
                                <a href='{#(operation() {model.numClicks++;})}'>
                                    Click
                                </a>
                             </html>"
                },
                Label {
                    text: bind "Count: {model.numClicks}"
                }]

We are using the "bind" keyword, which is a lazy instantiation mechanism. Here, we are binding the numClicks to the hyperlink available between the <html> tags. On compiling the code, you see a screen like this.


When you click on the hyperlink a few times, the count gets incremented and is diplayed on the frame as you could see from the following screenshot.


 

Comments:

Post a Comment:
  • HTML Syntax: NOT allowed