在BorderPane中使用JFX No Scrollbar

时间:2017-05-20 09:53:33

标签: java javafx javafx-8

我正在尝试在边框内布置表格。当我这样做时,缩小窗口会截断表格并且不显示滚动条。

这是布局:

<BorderPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="600.0" prefWidth="1000.0" xmlns="http://javafx.com/javafx/8.0.111" xmlns:fx="http://javafx.com/fxml/1" fx:controller="javafxapplication1.SimpleUIController">
<center>
<AnchorPane>
      <TableView fx:id="table" prefHeight="500.0" prefWidth="1000.0" AnchorPane.bottomAnchor="10.0" AnchorPane.leftAnchor="10.0" AnchorPane.rightAnchor="50.0" AnchorPane.topAnchor="10.0" BorderPane.alignment="CENTER">
        <columns>
          <TableColumn fx:id="colName" prefWidth="500.0" text="Name" />
          <TableColumn fx:id="colPrice" prefWidth="400.0" text="Price" />
        </columns>
      </TableView>
</AnchorPane>
</center>
</BorderPane>

这就是窗口比桌子小的样子:enter image description here

如果我像这样删除边框窗格:

<AnchorPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="600.0" prefWidth="1000.0" xmlns="http://javafx.com/javafx/8.0.111" xmlns:fx="http://javafx.com/fxml/1" fx:controller="javafxapplication1.SimpleUIController">
      <TableView fx:id="table" prefHeight="500.0" prefWidth="1000.0" AnchorPane.bottomAnchor="10.0" AnchorPane.leftAnchor="10.0" AnchorPane.rightAnchor="50.0" AnchorPane.topAnchor="10.0">
        <columns>
          <TableColumn fx:id="colName" prefWidth="500.0" text="Name" />
          <TableColumn fx:id="colPrice" prefWidth="400.0" text="Price" />
        </columns>
      </TableView>
</AnchorPane>

然后我根据需要获得滚动条:enter image description here

使用边框时如何获取滚动条?

1 个答案:

答案 0 :(得分:0)

尝试将ScrollPane设置为父节点,如下所示:

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

<?import javafx.scene.control.ScrollPane?>
<?import javafx.scene.control.TableColumn?>
<?import javafx.scene.control.TableView?>
<?import javafx.scene.layout.BorderPane?>
<?import javafx.scene.layout.HBox?>
<?import javafx.scene.layout.VBox?>


<HBox
    maxHeight="1.7976931348623157E308"
    maxWidth="1.7976931348623157E308"
    xmlns="http://javafx.com/javafx/8.0.111"
    xmlns:fx="http://javafx.com/fxml/1">
    <children>
        <VBox
            maxHeight="1.7976931348623157E308"
            maxWidth="1.7976931348623157E308"
            HBox.hgrow="ALWAYS">
            <children>
                <ScrollPane
                    fitToHeight="true"
                    fitToWidth="true"
                    maxHeight="1.7976931348623157E308"
                    maxWidth="1.7976931348623157E308"
                    VBox.vgrow="ALWAYS">
                    <content>
                        <BorderPane
                            maxHeight="1.7976931348623157E308"
                            maxWidth="1.7976931348623157E308"
                            minHeight="-Infinity"
                            minWidth="-Infinity">
                            <center>
                                <TableView
                                    maxHeight="1.7976931348623157E308"
                                    maxWidth="1.7976931348623157E308">
                                    <columns>
                                        <TableColumn
                                            maxWidth="1.7976931348623157E308"
                                            prefWidth="400.0"
                                            text="C1" />
                                        <TableColumn
                                            maxWidth="1.7976931348623157E308"
                                            prefWidth="500.0"
                                            text="C2" />
                                    </columns>
                                    <columnResizePolicy>
                                        <TableView fx:constant="CONSTRAINED_RESIZE_POLICY" />
                                    </columnResizePolicy>
                                </TableView>
                            </center>
                        </BorderPane>
                    </content>
                </ScrollPane>
            </children>
        </VBox>
    </children>
</HBox>

Screenshot