JavaFX ScrollPane

104 篇文章 5 订阅

The JavaFX ScrollPane control is a container that has two scrollbars around the component it contains if the component is larger than the visible area of the ScrollPane. The scrollbars enable the user to scroll around the component shown inside the ScrollPane, so different parts of the component can be seen. The JavaFX ScrollPane controls is represented by the JavaFX class javafx.scene.control.ScrollPane. Here is a screenshot of a JavaFX ScrollPane with a JavaFX ImageView inside:

Create a ScrollPane

To use a JavaFX ScrollPane you must first create a ScrollPane instance. Here is an example of creating a JavaFX ScrollPane instance:

ScrollPane scrollPane = new ScrollPane();

Set ScrollPane Content

Once you have created a JavaFX ScrollPane instance you can set the content you want it to display via its setContent() method. Here is an example that sets a JavaFX ImageView as content of a JavaFX ScrollPane:

ScrollPane scrollPane = new ScrollPane();

String imagePath = "images/aerial-beverage-caffeine-972533.jpg";
ImageView imageView = new ImageView(new Image(new FileInputStream(imagePath)));

scrollPane.setContent(imageView);

ScrollPane Viewport

The visible part of a JavaFX ScrollPane is called the ScrollPane viewport. As you scroll around the content displayed inside the ScrollPane using the scrollbars, the viewport is moved around the content too, making different parts of the content visible.

Content With Effects or Transforms

If the content (JavaFX control) you want to display inside the JavaFX ScrollPane uses effects or transforms, you must first wrap these controls in a JavaFX Group. Otherwise the content won't be displayed correctly.

Pannable ScrollPane

By default the user can only navigate around the content displayed in a JavaFX ScrollPane using its scrollbars. However, it is possible to make a JavaFX ScrollPane pannable. A pannable ScrollPane enables the user to navigate its content by holding down the left mouse button and move the mouse around. This will have the same effect as using the scrollbars. However, using panning you can move the content along both X and Y axis simultaneously. This is not possible using the scrollbars, where the user can only operate one scrollbar at a time.

To switch a JavaFX ScrollPane into pannable mode you must set its pannableProperty to the value true. Here is an example of switching a JavaFX ScrollPane into pannable mode:

scrollPane.pannableProperty().set(true);

Fit To Width

The JavaFX ScrollPane fitToWidth property can make the ScrollPane fit its content to the width of the ScrollPane viewport. To do so, the fitToWidth property must be set to the value true. This property is ignored if the content node is not resizable. Here is an example of setting the JavaFX ScrollPane fitToWidth property to true:

scrollPane.fitToWidthProperty().set(true);

Fit To Height

The JavaFX ScrollPane fitToHeight property can make the ScrollPane fit its content to the height of the ScrollPane viewport. To do so, the fitToHeight property must be set to the value true. This property is ignored if the content node is not resizable. Here is an example of setting the JavaFX ScrollPane fitToHeight property to true:

scrollPane.fitToHeightProperty().set(true);

Showing and Hiding Scrollbars via ScrollBar Policies

It is possible to specify when the JavaFX ScrollPane is to show the vertical and horizontal scrollbars. You do so via the ScrollPane hbarPolicyProperty and vbarPolicyProperty properties. These properties can be set to one of the ScrollPane.ScrollBarPolicy enum values. You can choose from the values ALWAYS, AS_NEEDED and NEVER. Here is an example of setting the hbarPolicyProperty and vbarPolicyProperty to ScrollBarPolicy.NEVER:

scrollPane.hbarPolicyProperty().setValue(ScrollPane.ScrollBarPolicy.NEVER);
scrollPane.vbarPolicyProperty().setValue(ScrollPane.ScrollBarPolicy.NEVER);

The above example removes the vertical and horizonal scrollbar from the ScrollPane. Without the scrollbars the user cannot use them to scroll around the content of the ScrollPane. However, if the ScrollPane is in pannable mode (see earlier sections in this JavaFX ScrollPane tutorial) the user can still grab the content and scroll around it with the mouse.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值