16th back to javafx2.0 scroll bar scrollbar

Source: Internet
Author: User

Original address http://download.oracle.com/javafx/2.0/ui_controls/scrollbar.htm

 

 

ScrollBarClass allows you to create a scroll bar panel and a view. Figure
10-1 shows three parts of the scroll bar: the scroll bar (Thumb), the left and right buttons (or the up and down buttons), and the scroll area.

Figure 10-1 elements of the scroll bar


Description of "Figure 10-1 elements of the scroll bar"

Create scroll bar

Take a moment to look at the code block in Example 10-1.

Example 10-1 simple scroll bar

ScrollBar sc = new ScrollBar();sc.setMin(0);sc.setMax(100);sc.setValue(50);

 setMin And setmaxThe method defines the minimum and maximum values displayed in the scroll bar. When you move the scroll bar, its value changes. In Example
In 10-1, the value is 50, so the scroll bar is located in the middle of the scroll bar after the application starts. The scroll bar is horizontal by default.setOrientationMethod to set it to the vertical direction.

You can click the left and right buttons (vertical scroll bar is the upper and lower buttons) to change the value according to the growth unit.The unit_increment attribute specifies the range of the scroll bar adjustment when the button is clicked. You can also click the scroll area to make a big change.The block_increment attribute defines the range of changes when you click the scroll area.

In your application, you can use several scroll bars to scroll the graphic content beyond the boundary of the available space.

Use scroll bar in applications

In practice. The application created in Example 10-2 implements a scroll window to view the image. The purpose of this application is to allow users to view the content in the vertical box, which is longer than the window height.

Example 10-2 scrolling through multiple images

import javafx.application.Application;import javafx.beans.value.ChangeListener;import javafx.beans.value.ObservableValue;import javafx.geometry.Orientation;import javafx.scene.Group;import javafx.scene.Scene;import javafx.scene.control.ScrollBar;import javafx.scene.effect.DropShadow;import javafx.scene.image.Image;import javafx.scene.image.ImageView;import javafx.scene.layout.VBox;import javafx.scene.paint.Color;import javafx.stage.Stage;public class Main extends Application {     final ScrollBar sc = new ScrollBar();    final Image[] images = new Image[5];    final ImageView[] pics = new ImageView[5];    final VBox vb = new VBox();    DropShadow shadow = new DropShadow();     @Override    public void start(Stage stage) {        Group root = new Group();        Scene scene = new Scene(root, 180, 180);        scene.setFill(Color.BLACK);        stage.setScene(scene);        stage.setTitle("Scrollbar");        root.getChildren().addAll(vb, sc);         shadow.setColor(Color.GREY);        shadow.setOffsetX(2);        shadow.setOffsetY(2);         vb.setLayoutX(5);        vb.setSpacing(10);         sc.setLayoutX(scene.getWidth()-sc.getWidth());        sc.setMin(0);        sc.setOrientation(Orientation.VERTICAL);        sc.setPrefHeight(180);        sc.setMax(360);         for (int i = 0; i < 5; i++) {            final Image image = images[i] =                new Image(getClass().getResourceAsStream(                    "fw" +(i+1)+ ".jpg")                );            final ImageView pic = pics[i] =                new ImageView(images[i]);            pic.setEffect(shadow);            vb.getChildren().add(pics[i]);        }         sc.valueProperty().addListener(new ChangeListener<Number>() {            public void changed(ObservableValue<? extends Number> ov,                Number old_val, Number new_val) {                    vb.setLayoutY(-new_val.doubleValue());            }        });         stage.show();    }     public static void main(String[] args) {        launch(args);    }}

The first line of the Code adds a vertical box with an image and a scroll bar to the scenario.

Vertical boxY coordinates will follow the scroll bar VALUEAttribute.So every time the scroll bar moves (or the button or scroll area), the vertical box moves. See example
10-3.

Example 10-3 implementing the scrolling of the vertical box

sc.valueProperty().addListener(new ChangeListener<Number>() {    public void changed(ObservableValue<? extends Number> ov,        Number old_val, Number new_val) {            vb.setLayoutY(-new_val.doubleValue());        }});

Compile and run the application, as shown in Figure 10-2.

Figure 10-2 scroll bar sample


Description of "Figure 10-2 scroll bar sample"

This application isScrollBarClass. You can also use a customized class to create a rolling area in a scenario. Because for each UI control and node (with node class), you can modify the appearance of the scroll bar from the default implementation.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.