Java Reference
In-Depth Information
computing the loan payments. This class was introduced in Listing 10.2, Loan.java. Invok-
ing loan.getMonthlyPayment() returns the monthly payment for the loan (line 71). The
String.format method, introduced in Section 10.10.7, is used to format a number into a
desirable format and returns it as a string (lines 70, 72). Invoking the setText method on a
text field sets a string value in the text field.
15.8 Mouse Events
A MouseEvent is fired whenever a mouse button is pressed, released, clicked, moved,
or dragged on a node or a scene.
Key
Point
The MouseEvent object captures the event, such as the number of clicks associated with it,
the location (the x - and y -coordinates) of the mouse, or which mouse button was pressed, as
shown in Figure 15.10.
javafx.scene.input.MouseEvent
+getButton(): MouseButton
+getClickCount(): int
+getX(): double
+getY(): double
+getSceneX(): double
+getSceneY(): double
+getScreenX(): double
+getScreenY(): double
+isAltDown(): boolean
+isControlDown(): boolean
+isMetaDown(): boolean
+isShiftDown(): boolean
Indicates which mouse button has been clicked.
Returns the number of mouse clicks associated with this event.
Returns the x -coordinate of the mouse point in the event source node.
Returns the y -coordinate of the mouse point in the event source node.
Returns the x -coordinate of the mouse point in the scene.
Returns the y -coordinate of the mouse point in the scene.
Returns the x -coordinate of the mouse point in the screen.
Returns the y -coordinate of the mouse point in the screen.
Returns true if the Alt key is pressed on this event.
Returns true if the Control key is pressed on this event.
Returns true if the mouse Meta button is pressed on this event.
Returns true if the Shift key is pressed on this event.
F IGURE 15.10
The MouseEvent class encapsulates information for mouse events.
Four constants— PRIMARY , SECONDARY , MIDDLE , and NONE —are defined in MouseButton
to indicate the left, right, middle, and none mouse buttons. You can use the getButton() method
to detect which button is pressed. For example, getButton() == MouseButton.SECONDARY
indicates that the right button was pressed.
The mouse events are listed in Table 15.1. To demonstrate using mouse events, we give
an example that displays a message in a pane and enables the message to be moved using a
mouse. The message moves as the mouse is dragged, and it is always displayed at the mouse
point. Listing 15.7 gives the program. A sample run of the program is shown in Figure 15.11.
detect mouse buttons
F IGURE 15.11
You can move the message by dragging the mouse.
L ISTING 15.7
MouseEventDemo.java
1 import javafx.application.Application;
2 import javafx.scene.Scene;
3 import javafx.scene.layout.Pane;
VideoNote
Move message using the
mouse
 
 
 
Search WWH ::




Custom Search