JavaFX SDK 1.2 introduces a new set of user-interface (UI) control components for JavaFX programmers. Previously, JavaFX UI Components simply "borrowed" their functionality from the underlying Swing components, which prevented their use in anything other than the desktop profile. The JavaFX SDK 1.2 components, however, take advantage of the more powerful JavaFX scene graph, which not only increases portability, but also allows JavaFX programmers to create more compelling graphical capabilities. In Part One in this series we looked at new layout classes in Java FX. Previous articles have examined RSS and storage, and JavaFX charts. Here, we take a closer look at the Button classes. Nodes and the Scene GraphJavaFX UI components all inherit from the Node class, which contains a wealth of properties common to all graphical components that can work with JavaFX's scene graph. What is a scene graph? According to the official documentation, a scene graph is a set of tree data structures in which each node is either a leaf node with no sub-nodes or a branch node with possible sub-nodes. In addition, each node may or may not have a parent node. Here are some other rules and characteristics taken directly from the JavaFX documentation:
The ButtonBase Class
Before we jump into the various styles of buttons that are available in JavaFX, let's introduce their abstract superclass:
The Button Class
First, let's take a look at the
import javafx.stage.Stage;
import javafx.scene.Scene;
import javafx.scene.text.Font;
import javafx.scene.control.Button;
Stage {
title: "Application title"
scene: Scene {
width: 250
height: 80
content: [
Button {
text: "OK"
translateX: 10
translateY: 10
strong: true
font: Font {size: 24 name: "Tahoma"}
action: function() {
println("Button pressed!");
}
}
]
}
}
Attached to the action property is an anonymous function that simply prints out a message when the button is pressed. However, you can customize the function to perform whatever action your program deems necessary. Figure 1 shows the resulting button, with and without the strong attribute selected. ![]() Figure 1. A Button, without and with the strong property enabled
The CheckBox Class
Next, let's look at an example of the
import javafx.stage.Stage;
import javafx.scene.Scene;
import javafx.scene.control.CheckBox;
Stage {
title: "Application title"
scene: Scene {
width: 250
height: 80
content: [
CheckBox {
text: "Check me!"
allowTriState: true
defined: false
selected: true
}
]
}
}
The resulting CheckBox example is shown in Figure 2. Note that we have shown each of the three states. ![]() Figure 2. A CheckBox, with the dashed, checked, and unchecked state
The RadioButton Class
Next, let's look at the
Here is a short example of how to define a RadioButton with a ToggleGroup.
import javafx.stage.Stage;
import javafx.scene.Scene;
import javafx.scene.control.RadioButton;
import javafx.scene.control.ToggleGroup;
import javafx.scene.layout.VBox;
def toggleGroup1:ToggleGroup = ToggleGroup {}
def radioButton1:RadioButton = RadioButton {
text: "Click me!"
selected: true
toggleGroup: toggleGroup1
}
def radioButton2:RadioButton = RadioButton {
text: "No, Click me!"
toggleGroup: toggleGroup1
}
def radioButton3:RadioButton = RadioButton {
text: "Please Click me!"
toggleGroup: toggleGroup1
}
Stage {
title: "Application title"
scene: Scene {
width: 250
height: 80
content: [
VBox {
content: [
radioButton1, radioButton2, radioButton3
]
}
]
}
}
The result is shown in Figure 3. Note that selecting any of the three buttons will "unselect" the others. ![]() Figure 3. Three RadioButton classes, each sharing a ToggleGroup to enforce mutual exclusivity
The ToggleButton Class
As we mentioned earlier with the RadioButton, the
You may have noticed that the properties of RadioButton and ToggleButton are identical. This means that, in addition to RadioButton classes, you can also create ToggleButton classes that belong to a ToggleGroup. If this is done, the behavior is the same: ToggleGroup will enforce a mutual exclusivity with either type of button. Here is an example of the ToggleButton class in action.
import javafx.stage.Stage;
import javafx.scene.Scene;
import javafx.scene.control.ToggleButton;
Stage {
title: "Application title"
scene: Scene {
width: 250
height: 80
content: [
ToggleButton {
text: "First"
}
]
}
}
The result is shown in Figure 4, in both its selected and unselected state. ![]() Figure 4. A ToggleButton outside of a ToggleGroup, in both its non-selected and selected state
The Hyperlink ClassOur final button class is one that may surprise Swing programmers: the Hyperlink class. A hyperlink is an HTML-like text label which can respond to rollovers and clicks, similar to a browser. In effect, it is a featureless button (text or image) that has an action function attached to it. Table 5 shows the properties of the Hyperlink class.
Note that there is a visited property, which indicates whether the hyperlink should indicate if it has already been visited. Here is an example of how to use Hyperlink.
import javafx.stage.Stage;
import javafx.scene.Scene;
import javafx.scene.control.Hyperlink;
Stage {
title: "Application title"
scene: Scene {
width: 250
height: 80
content: [
Hyperlink {
text: "http://www.javafx.com"
visited: true
action: function() {
println("Hyperlink pressed!");
}
}
]
}
}
The result is shown in Figure 5. Note that when you press on the text, the action function is called, which will allow you to take the appropriate action, such as re-rendering the display or opening a browser and moving to that page. ![]() Figure 5. A HyperLink node
Summary: More Compelling GraphicsThe new set of user-interface (UI) control components for JavaFX programmers found in JavaFX SDK 1.2 enables developers to take advantage of the JavaFX scene graph, which as we have seen, enhances the flexibility and functionality of the Button classes. This, along with the new layout classes, enables JavaFX developers to create more compelling and portable graphical capabilities. For More Information
Rate This ArticleDiscussionWe welcome your participation in our community. Please keep your comments civil and on point. You can optionally provide your email address to be notified of repliesyour information is not used for any other purpose. By submitting a comment, you agree to these Terms of Use. |
Robert Eckstein has worked with Java technology since its first release. He was formerly a programmer and editor for O'Reilly Media, Inc., a programmer for Motorola's cellular technology division, and previously worked for Sun Microsystems as a senior staff writer on java.sun.com.
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Oracle is reviewing the Sun product roadmap and will provide guidance to customers in accordance with Oracle's standard product communication policies. Any resulting features and timing of release of such features as determined by Oracle's review of roadmaps, are at the sole discretion of Oracle. All product roadmap information, whether communicated by Sun Microsystems or by Oracle, does not represent a commitment to deliver any material, code, or functionality, and should not be relied upon in making purchasing decisions. It is intended for information purposes only, and may not be incorporated into any contract.
|
| ||||||||||||