JavaFX ScrollPane的水平滚动条隐藏内容

时间:2018-03-20 14:05:19

标签: java javafx javafx-8

我将一些内容包装到ScrollPane中,因为如果内容不适合屏幕,我想要一个水平滚动条。

只要不需要滚动条,一切都很好: Without scrollbar, everything is well visible

然而,当显示滚动条时,它(垂直)隐藏了部分内容: With scroll bar, parts of the content are not visible

如何防止此行为?内容应始终完整显示。我尝试使用fitToHeight="true",但这没有帮助。

遵循一些示例FXML(添加多层HBox和VBox以模仿我的实际应用程序的结构):

<BorderPane>
    <top>
        <ScrollPane vbarPolicy="NEVER" fitToHeight="true">
            <HBox>
                <VBox>
                    <TitledPane text="Title">
                        <HBox spacing="100.0">
                            <Text text="Test1 Test2 Test3 Test4"></Text>
                            <Text text="Test1 Test2 Test3 Test4"></Text>
                            <Text text="Test1 Test2 Test3 Test4"></Text>
                            <Text text="Test1 Test2 Test3 Test4"></Text>
                            <Text text="Test1 Test2 Test3 Test4"></Text>
                            <Text text="Test1 Test2 Test3 Test4"></Text>
                            <Text text="Test1 Test2 Test3 Test4"></Text>
                            <Text text="Test1 Test2 Test3 Test4"></Text>
                            <Text text="Test1 Test2 Test3 Test4"></Text>
                            <Text text="Test1 Test2 Test3 Test4"></Text>
                            <Text text="Test1 Test2 Test3 Test4"></Text>
                        </HBox>
                    </TitledPane>
                </VBox>
            </HBox>
        </ScrollPane>
    </top>
    <center>

    </center>
    <bottom>

    </bottom>
</BorderPane>

3 个答案:

答案 0 :(得分:2)

看起来像ScrollPaneSkin中的错误(reported):如果策略为AS_NEEDED并且scrollBar可见,则其computePrefHeight方法不会考虑scrollBar的高度。

所以解决方法是一个自定义皮肤;)注意,如果策略从ALWAYS更改为AS_NEEDED(在调用computeXX时,条形图是可见的 - 不太确定为什么这样做是不够的),所以我们正在听政策的变化并隐藏吧..粗鲁但有效。

自定义皮肤(当心:不正式测试!)和玩的驱动程序:

public class ScrollPaneSizing extends Application{

    public static class DebugScrollPaneSkin extends ScrollPaneSkin {

        public DebugScrollPaneSkin(ScrollPane scroll) {
            super(scroll);
            registerChangeListener(scroll.hbarPolicyProperty(), p -> {
                // rude .. but visibility is updated in layout anyway
                getHorizontalScrollBar().setVisible(false);
            });
        }

        @Override
        protected double computePrefHeight(double x, double topInset,
                double rightInset, double bottomInset, double leftInset) {
            double computed = super.computePrefHeight(x, topInset, rightInset, bottomInset, leftInset);
            if (getSkinnable().getHbarPolicy() == ScrollBarPolicy.AS_NEEDED && getHorizontalScrollBar().isVisible()) {
                // this is fine when horizontal bar is shown/hidden due to resizing
                // not quite okay while toggling the policy
                // the actual visibilty is updated in layoutChildren?
                computed += getHorizontalScrollBar().prefHeight(-1);
            }
            return computed;
        }


    }

    private Parent createContent() {
        HBox inner = new HBox(new Text("somehing horizontal and again again ........")); 
        TitledPane titled = new TitledPane("my title", inner);
        ScrollPane scroll = new ScrollPane(titled) {

            @Override
            protected Skin<?> createDefaultSkin() {
                return new DebugScrollPaneSkin(this);
            }

        };
        scroll.setVbarPolicy(NEVER);
        scroll.setHbarPolicy(ALWAYS);
        // scroll.setFitToHeight(true);

        Button policy = new Button("toggle HBarPolicy");
        policy.setOnAction(e -> {
            ScrollBarPolicy p = scroll.getHbarPolicy();
            scroll.setHbarPolicy(p == ALWAYS ? AS_NEEDED : ALWAYS);
        });
        HBox buttons = new HBox(10, policy);
        BorderPane content = new BorderPane();
        content.setTop(scroll);
        content.setBottom(buttons);
        return content;
    }

    @Override
    public void start(Stage stage) throws Exception {
        stage.setScene(new Scene(createContent(), 400, 200));
        stage.setTitle(FXUtils.version());
        stage.show();
    }

    public static void main(String[] args) {
        launch(args);
    }

    @SuppressWarnings("unused")
    private static final Logger LOG = Logger
            .getLogger(ScrollPaneSizing.class.getName());

}

答案 1 :(得分:1)

您可以通过将vbox的minHeight设置为完全显示文本的大小来替代填充

来解决此问题。

离。(填充)

<?xml version="1.0" encoding="UTF-8"?>

<?import java.lang.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>
<?import javafx.scene.control.ColorPicker?>
<?import javafx.scene.layout.VBox?>

<?import javafx.scene.text.Text?>
<?import javafx.geometry.Insets?>
<BorderPane xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1">
    <top>
        <ScrollPane vbarPolicy="NEVER" fitToHeight="true">
            <HBox>
                <VBox spacing="100.0">
                    <TitledPane text="Title">
                        <HBox>
                            <children>
                                <Text text="Test1 Test2 Test3 Test4" />
                                <Text text="Test1 Test2 Test3 Test4" />
                                <Text text="Test1 Test2 Test3 Test4" />
                                <Text text="Test1 Test2 Test3 Test4" />
                                <Text text="Test1 Test2 Test3 Test4" />
                                <Text text="Test1 Test2 Test3 Test4" />
                                <Text text="Test1 Test2 Test3 Test4" />
                                <Text text="Test1 Test2 Test3 Test4" />
                                <Text text="Test1 Test2 Test3 Test4" />
                                <Text text="Test1 Test2 Test3 Test4" />
                                <Text text="Test1 Test2 Test3 Test4" />
                            </children>
                        </HBox>
                    </TitledPane>
                    <padding>
                        <Insets bottom="5.0" top="5.0" />
                    </padding>
                </VBox>
            </HBox>
        </ScrollPane>
    </top>
    <center>

    </center>
    <bottom>

    </bottom>
</BorderPane>

离。 (最小高度)

<?xml version="1.0" encoding="UTF-8"?>

<?import javafx.scene.control.ScrollPane?>
<?import javafx.scene.control.TitledPane?>
<?import javafx.scene.layout.BorderPane?>
<?import javafx.scene.layout.HBox?>
<?import javafx.scene.layout.VBox?>
<?import javafx.scene.text.*?>
<BorderPane xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1">
    <top>
        <ScrollPane vbarPolicy="NEVER" fitToHeight="true" minHeight="83.0">
            <HBox>
                <VBox>
                    <TitledPane text="Title">
                        <HBox>
                            <Text text="Test1 Test2 Test3 Test4"/>
                            <Text text="Test1 Test2 Test3 Test4"/>
                            <Text text="Test1 Test2 Test3 Test4"/>
                            <Text text="Test1 Test2 Test3 Test4"/>
                            <Text text="Test1 Test2 Test3 Test4"/>
                            <Text text="Test1 Test2 Test3 Test4"/>
                            <Text text="Test1 Test2 Test3 Test4"/>
                            <Text text="Test1 Test2 Test3 Test4"/>
                            <Text text="Test1 Test2 Test3 Test4"/>
                            <Text text="Test1 Test2 Test3 Test4"/>
                            <Text text="Test1 Test2 Test3 Test4"/>
                        </HBox>
                    </TitledPane>
                </VBox>
            </HBox>
        </ScrollPane>
    </top>
    <center>

    </center>
    <bottom>

    </bottom>
</BorderPane>

答案 2 :(得分:1)

我写了一个 openJDK 8 版本,因为接受的答案只适用于 9

public class ScrollPaneHSkin extends ScrollPane
{

  ScrollBar hbar;

  public ScrollPaneHSkin()
  {
    super();
  }

  public ScrollPaneHSkin(Node content)
  {
    super(content);
  }

  @Override
  protected Skin<?> createDefaultSkin()
  {
    return new HSkin();
  }


  private class HSkin extends ScrollPaneSkin
  {
    HSkin()
    {
      super(ScrollPaneHSkin.this);
      hbarPolicyProperty().addListener((ov, old, current) ->
      
        // rude .. but visibility is updated in layout anyway
        hsb.setVisible(false)
      );
    }
    
    @Override
    protected double computePrefHeight(double x, double topInset,
                                       double rightInset, double bottomInset, double leftInset)
    {
      double computed = super.computePrefHeight(x, topInset, rightInset, bottomInset, leftInset);
      if (getSkinnable().getHbarPolicy() == ScrollBarPolicy.AS_NEEDED && hsb.isVisible())
      {
        // this is fine when horizontal bar is shown/hidden due to resizing
        // not quite okay while toggling the policy
        // the actual visibilty is updated in layoutChildren?
        computed += hsb.prefHeight(-1);
      }
      return computed;
    }
  }
}