Java Reference
In-Depth Information
20 Pane paneForText = new Pane();
21 paneForText.getChildren().add(text);
22
23 // Create a border pane to hold text and scroll bars
24 BorderPane pane = new BorderPane();
25 pane.setCenter(paneForText);
26 pane.setBottom(sbHorizontal);
27 pane.setRight(sbVertical);
28
29
add text to a pane
border pane
// Listener for horizontal scroll bar value change
30
sbHorizontal.valueProperty().addListener(ov ->
31
text.setX(sbHorizontal.getValue() * paneForText.getWidth() /
set new location for text
32
sbHorizontal.getMax()));
33
34
// Listener for vertical scroll bar value change
35
sbVertical.valueProperty().addListener(ov ->
36
text.setY(sbVertical.getValue() * paneForText.getHeight() /
set new location for text
37
sbVertical.getMax()));
38
39 // Create a scene and place it in the stage
40 Scene scene = new Scene(pane, 450 , 170 );
41 primaryStage.setTitle( "ScrollBarDemo" ); // Set the stage title
42 primaryStage.setScene(scene); // Place the scene in the stage
43 primaryStage.show(); // Display the stage
44 }
45 }
The program creates a text (line 13) and two scroll bars ( sbHorizontal and sbVertical )
(lines 15-16). The text is placed in a pane (line 21) that is then placed in the center of the border
pane (line 25). If the text were directly placed in the center of the border pane, the position of the
text cannot be changed by resetting its x and y properties. The sbHorizontal and sbVertical
are placed on the right and at the bottom of the border pane (lines 26-27), respectively.
You can specify the properties of the scroll bar. By default, the property value is 100 for
max , 0 for min , 10 for blockIncrement , and 15 for visibleAmount .
A listener is registered to listen for the sbHorizontal value property change (lines
30-32). When the value of the scroll bar changes, the listener is notified by invoking the han-
dler to set a new x value for the text that corresponds to the current value of sbHorizontal
(lines 31-32).
A listener is registered to listen for the sbVertical value property change (lines 35-37).
When the value of the scroll bar changes, the listener is notified by invoking the handler to set
a new y value for the text that corresponds to the current value of sbVertical (lines 36-37).
Alternatively, the code in lines 30-37 can be replaced by using binding properties as
follows:
text.xProperty().bind(sbHorizontal.valueProperty().
multiply(paneForText.widthProperty()).
divide(sbHorizontal.maxProperty()));
text.yProperty().bind(sbVertical.valueProperty().multiply(
paneForText.heightProperty().divide(
sbVertical.maxProperty())));
16.31 How do you create a horizontal scroll bar? How do you create a vertical scroll bar?
16.32 How do you write the code to respond to the value property change of a scroll bar?
16.33 How do you get the value from a scroll bar? How do you get the maximum value
from a scroll bar?
Check
Point
 
 
Search WWH ::




Custom Search