Javafx
Javafx
Javafx
@Override
public void start(Stage primaryStage) throws Exception {
// TODO Auto-generated method stub
}
}
Cont..
• However, we can add various objects to this primary stage.
• The objects can only be added in a hierarchical way
• i.e. first, scene graph will be added to this primaryStage and then that
scene graph may contain the nodes.
• A node may be any object of the user's interface like text area, buttons,
shapes, media, etc.
Scene
// Create a scene.
Scene myScene = new Scene(rootNode, 300, 200);
// Set the scene on the stage.
myStage.setScene(myScene);
// Show the stage and its scene.
myStage.show();
}
// Override the stop() method.
public void stop() {
System.out.println("Inside the stop() method.");
}
}
A Simple JavaFX Control: Label
• The JavaFX label is an instance of the Label class, which is packaged
in javafx.scene.control.
• Label inherits Labeled and Control, among other classes.
• The Labeled class defines several features that are common to all
labeled elements (that is, those that can contain text), and Control
defines features related to all controls.
• Label defines three constructors. The one we will use here is
Label(String str) Here, str is the string that is displayed.
Using Buttons and Events
• One commonly used control is the button.
• This makes button events one of the most frequently handled.
• Button is a good way to demonstrate the fundamentals of event
handling in JavaFX. For this reason, the fundamentals of event
handling and the button are introduced together.
Event Basics
• The base class for JavaFX events is the Event class, which is packaged in
javafx.event.
• Event inherits java.util.EventObject, which means that JavaFX events share the
same basic functionality as other Java events.
• Several subclasses of Event are defined. The one that we will use here is
ActionEvent.
• It handles action events generated by a button. In general, JavaFX uses what is, in
essence, the delegation event model approach to event handling.
• To handle an event, you must first register the handler that acts as a listener for
the event.
• When the event occurs, the listener is called. It must then respond to the event and
return.
• Events are handled by implementing the EventHandler interface, which is also in
javafx.event.
Cont..
Introducing the Button Control
• In JavaFX, the push button control is provided by the Button class,
which is in javafx.scene.- control. Button inherits a fairly long list of
base classes that include ButtonBase, Labeled, Region, Control,
Parent, and Node.
Button defines three constructors.
Button(String str)
In this case, str is the message that is displayed in the button.
When a button is pressed, an ActionEvent is generated.
ActionEvent is packaged in javafx.event.
You can register a listener for this event by using setOnAction( ), which has this
general form:
final void setOnAction(EventHandler handler)
Here, handler is the handler being registered.
The setOnAction( ) method sets the property onAction, which stores a reference to
the handler.
As with all other Java event handling, your handler must respond to the event as fast
as possible and then return. If your handler consumes too much time, it will
noticeably slow down the application. For lengthy operations, you must use a
separate thread of execution.
Drawing Directly on a Canvas
• This is one of the most important ways that JavaFX improves on
Swing. As you may know, in Swing or the AWT, you must call the
repaint( ) method to cause a window to be repainted. Furthermore,
your application needs to store the window contents, redrawing them
when painting is requested. JavaFX eliminates this tedious mechanism
because it keeps track of what you display in a scene and redisplays
that scene as needed. This is called retained mode. With this approach,
there is no need to call a method like repaint( ). Rendering is
automatic.
Exploring JavaFX Controls
• The JavaFX control classes discussed in this chapter are shown here:
• Button
• ListView
• TextField
• CheckBox
• RadioButton
• ToggleButton
• Label
• ScrollPane
• TreeView
• These and the other JavaFX controls are packaged in javafx.scene.control.
IMAGES
• At the foundation for JavaFX’s support for images are two classes:
• Image and ImageView.
• Image encapsulates the image, itself
• ImageView manages the display of an image.
• Both classes are packaged in javafx.scene.image
Cont..
• The Image class loads an image from either an InputStream, a URL, or a path to
the image file.
• Image defines several constructors; this is the one we will use: Image(String url)
• Here, url specifies a URL or a path to a file that supplies the image.
• Once you have an Image, you will use ImageView to display it.
• ImageView is derived from Node, which means that it can be part of a scene
graph.
• ImageView defines three constructors.
• The first one we will use is shown here:
• ImageView(Image image)
• This constructor creates an ImageView that uses image for its image.
In the program, pay close attention to the following sequence that
loads the image and then creates an ImageView that uses that
image:
// Create an image.
Image hourglass = new Image("HourGlass.png");
// Create an image view that uses the image.
ImageView hourglassIV = new ImageView(hourglass);
Excercise
• Adding an Image to a Label
• Using an Image with a Button
ToggleButton
• A useful variation on the push button is called the toggle button.
• A toggle button looks just like a push button, but it acts differently because it has two states:
pushed and released.
• That is, when you press a toggle button, it stays pressed rather than popping back up as a regular
push button does.
• When you press the toggle button a second time, it releases (pops up). Therefore, each time a
toggle button is pushed, it toggles between these two states.
• In JavaFX, a toggle button is encapsulated in the ToggleButton class.
• It implements the Toggle interface, which defines functionality common to all types of two-state
buttons. ToggleButton defines three constructors.
• ToggleButton(String str) Here, str is the text displayed in the button. Another constructor allows
you to include an image. Like other buttons, a ToggleButton generates an action event when it is
pressed. Because ToggleButton defines a two-state control, it is commonly used to let the user
select an option
Cont..
• To do this, use the isSelected( ) method, shown here:
• final boolean isSelected( )
• It returns true if the button is pressed and false otherwise
RadioButton
• Another type of button provided by JavaFX is the radio button.
• Radio buttons are a group of mutually exclusive buttons, in which only one button
can be selected at any one time.
• They are supported by the RadioButton class, which extends both ButtonBase and
ToggleButton.
• It also implements the Toggle interface.
• Thus, a radio button is a specialized form of a toggle button. You have almost
certainly seen radio buttons in action because they are the primary control
employed when the user must select only one option among several alternatives.
• To create a radio button, we will use the following constructor:
• RadioButton(String str)
• Here, str is the label for the button. Like other buttons, when a RadioButton is
used, an action event is generated
CheckBox
• The CheckBox class encapsulates the functionality of a check box.
• Its immediate superclass is ButtonBase.
• Although you are no doubt familiar with check boxes because they are widely used controls, the
JavaFX check box is a bit more sophisticated than you may at first think. This is because
CheckBox supports three states.
• The first two are checked or unchecked, as you would expect, and this is the default behavior.
• The third state is indeterminate (also called undefined). It is typically used to indicate that the state
of the check box has not been set or that it is not relevant to a specific situation. If you need the
indeterminate state, you will need to explicitly enable it.
• CheckBox defines two constructors. The first is the default constructor. The second lets you
specify a string that identifies the box.
• It is shown here: CheckBox(String str) It creates a check box that has the text specified by str as a
label. As with other buttons, a CheckBox generates an action event when it is selected
ListView
• Another commonly used control is the list view, which in JavaFX is encapsulated by
ListView.
• List views are controls that display a list of entries from which you can select one or
more.
• Because of their ability to make efficient use of limited screen space, list views are
popular alternatives to other types of selection controls.
• ListView is a generic class that is declared like this: class ListView<T>
• Here, T specifies the type of entries stored in the list view.
• Often, these are entries of type String, but other types are also allowed.
• ListView defines two constructors. The first is the default constructor, which creates an
empty ListView.
• The second lets you specify the list of entries in the list.
• It is shown here: ListView(ObservableList list) Here, list specifies a list of the items that
will be displayed.
• It is an object of type ObservableList, which defines a list of observable objects. It
inherits java.util.List.
// Demonstrate a list view.
import javafx.application.*;
import javafx.scene.*;
import javafx.stage.*;
import javafx.scene.layout.*;
import javafx.scene.control.*;
import javafx.geometry.*;
import javafx.beans.value.*;
import javafx.collections.*;
public class ListViewDemo extends Application {
Label response;
ComboBoX
• A control related to the list view is the combo box, which is implemented in
JavaFX by the ComboBox class.
• A combo box displays one selection, but it will also display a drop-down list that
allows the user to select a different item.
• You can also allow the user to edit a selection. ComboBox inherits
ComboBoxBase, which provides much of its functionality.
• Unlike the ListView, which can allow multiple selections, ComboBox is designed
for single-selection. ComboBox is a generic class that is declared like this:
class ComboBox<T>
Here, T specifies the type of entries.
Often, these are entries of type String, but other types are also allowed.
• ComboBox defines two constructors. The first is the default
constructor, which creates an empty ComboBox.
• The second lets you specify the list of entries. It is shown here:
• ComboBox(ObservableList<T> list)
• In this case, list specifies a list of the items that will be displayed. It is
an object of type ObservableList, which defines a list of observable
objects.
END