Original address http://download.oracle.com/javafx/2.0/ui_controls/scrollpane.htm
The scroll window provides a view for UI elements to be viewed in scrolling mode. This control allows you to view the information by moving the viewport or scroll bar. Figure 11-1 is a scroll window with the default settings for adding images.
Figure 11-1 scroll pane
Description of "Figure 11-1 scroll pane"
Create scroll pane
Example 11-1 demonstrates how to create a scroll window in an application.
Example 11-1 using a scroll pane to view an image
Image roses = new image (getclass (). getresourceasstream ("roses.jpg"); scrollpane sp = new scrollpane (); sp. setnode (New imageview (roses ));
Setnode
The method defines the content of nodes in the tumble window. You can specify only one node. To create a tumble window with multiple components, you must use a layout container orGroup class. Can be
Setpannable
Method settingsTrue, so that when you click and move the mouse to preview the image, the position of the scroll bar will change accordingly.
Set a scroll bar policy for scroll pane
The scrollpane class provides a policy to determine when to display a scroll bar: Always, never, when needed (
Always, never, needed). Use
Sethbarpolicy
AndSetvbarpolicy
Method: specify a policy for the horizontal and vertical scroll bars. In this way, Example
The vertical scroll bar in 11-2 is always displayed, while the horizontal scroll bar is not displayed.
Example 11-2 setting the horizontal and vertical scroll bar policies
Sp. sethbarpolicy (scrollbarpolicy. Never); sp. setvbarpolicy (scrollbarpolicy. Always );
The result is that you can only scroll the image vertically, as shown in Figure 11-2.
Figure 11-2 disabling the horizontal scroll bar
Description of "Figure 11-2 disabling the horizontal scroll bar"
Change the component size in scroll pane
When designing the UI interface, you may need to be able to change the size of components so that they can fit the width and height of the tumble window. IsSetfittowidth
OrSetfittoheight
Method settingsTrue
Value to match a specific direction.
The scroll window in Figure 11-3 contains the single choice button, text box, and password box. The size of the content exceeds the pre-defined size of the scroll window, so the vertical scroll bar is displayed. HoweverSetfittowidth
Method is setTrue: The window width is scaled so that the horizontal direction does not have a scroll bar.
Figure 11-3 fitting the width of the scroll pane
Description of "Figure 11-3 fitting the width of the scroll pane"
DefaultFit_to_width
AndFit_to_height
All attributesFalse: the original size of the content that can be changed. From aboveCodeRemove
Setfittowidth
Method, as shown in figure
11-4.
Figure 11-4 default properties for fitting the content
Description of "Figure 11-4 default properties for fitting the content"
Scrollpane
Class to retrieve and set the current, minimum, and maximum values of the content in the horizontal and vertical directions. Learn how to use it
Example of using scroll pane Program
Example 11-3 use a scroll window to display a vertical box with an image.Scrollpane
ClassThe vvalue attribute helps identify the displayed image and display its name.
Example 11-3 using a scroll pane to view images
Package scrollpanesample; import javafx. application. application; import javafx. beans. value. changelistener; import javafx. beans. value. observablevalue; import javafx. scene. scene; import javafx. scene. control. label; import javafx. scene. control. scrollpane; import javafx. scene. image. image; import javafx. scene. image. imageview; import javafx. scene. layout. priority; import javafx. scene. layout. vbox; import javafx. stage. Stage; public class main extends application {final scrollpane sp = new scrollpane (); final image [] images = new image [5]; final imageview [] pics = new imageview [5]; Final vbox VB = new vbox (); Final label filename = new label (); final string [] imagenames = new string [] {"fw1.jpg", "fw2.jpg", "fw3.jpg", "fw4.jpg", "fw5.jpg "}; @ override public void start (stage) {vbox box = new vbox (); SC ENE scene = new scene (box, 180,180); stage. setscene (scene); stage. settitle ("scroll pane"); box. getchildren (). addall (SP, filename); vbox. setvgrow (SP, priority. always); filename. setlayoutx (30); filename. setlayouty (160); For (INT I = 0; I <5; I ++) {images [I] = new image (getclass (). getresourceasstream (imagenames [I]); pics [I] = new imageview (images [I]); pics [I]. setfitwidth (100); pics [I]. setpreserve Ratio (true); Vb. getchildren (). add (PICS [I]);} sp. setvmax (440); SP. setprefsize (115,150); SP. setcontent (VB); SP. vvalueproperty (). addlistener (New changelistener <number> () {public void changed (observablevalue <? Extends number> ov, number old_val, number new_val) {filename. settext (imagenames [(new_val.intvalue ()-1)/100]) ;}}); stage. show () ;}public static void main (string [] ARGs) {launch (ARGs );}}
Figure 11-5 is the effect of compiling and running.
Figure 11-5 scrolling Images
Description of "Figure 11-5 scrolling images"
The maximum value of the vertical scroll bar is equal to the height of the vertical box. The code block in example 11-4 shows the name of the currently displayed image.
Example 11-4 tracking the change of the scroll pane's vertical Value
Sp. vvalueproperty (). addlistener (New changelistener <number> () {public void changed (observablevalue <? Extends number> ov, number old_val, number new_val) {filename. settext (imagenames [(new_val.intvalue ()-1)/100]) ;}});
The imageview object limits the Image Height to 100 points. So,
New_val.intvalue ()-1
The result of dividing by 100 shows the index of the current image.
You can change the minimum and maximum values of the horizontal scroll bar in the application to dynamically update the user interface.