动态添加图表在窗格上并适合其父窗口

时间:2015-10-09 07:28:05

标签: java javafx-8

我在javafx 8 dynamicaly中添加了一个图表。图表是正常添加的,但它不适合父母。这是代码:

<AnchorPane fx:id="anchorPaneSessionChartTab" prefHeight="534.0" prefWidth="951.0" xmlns="http://javafx.com/javafx/8.0.40" xmlns:fx="http://javafx.com/fxml/1" fx:controller="com.nokia.avalanche.client.controller.SessionChartTabController">
   <children>
      <VBox layoutX="-3.0" prefHeight="534.0" prefWidth="951.0">
        <children>
            <Pane fx:id="chartPane" prefHeight="393.0" prefWidth="951.0" />
            <TableView fx:id="tableViewStatisticsOverview" prefHeight="92.0" prefWidth="791.0">
              <columns>
                <TableColumn fx:id="tableColumnStatisticName" prefWidth="558.9999694824219" text="Mesaurement Name" />
                <TableColumn fx:id="tableColumnShowSeries" minWidth="3.0" prefWidth="63.0" text="Show" />
                  <TableColumn prefWidth="328.0" text="Action" />
              </columns>
            </TableView>
            <HBox nodeOrientation="RIGHT_TO_LEFT" prefHeight="26.0" prefWidth="791.0">
               <children>
                  <Button fx:id="btnSaveChart" mnemonicParsing="false" onAction="#btnSaveChartClicked" prefHeight="25.0" prefWidth="115.0" text="Save Chart">
                     <HBox.margin>
                        <Insets left="10.0" right="10.0" />
                     </HBox.margin>
                  </Button>
                  <Button fx:id="btnDeleteChart" mnemonicParsing="false" onAction="#btnDeleteChartClicked" prefHeight="25.0" prefWidth="115.0" text="Delete Chart" />
               </children>
               <VBox.margin>
                  <Insets top="5.0" />
               </VBox.margin>
            </HBox>
         </children>
      </VBox>
   </children>
</AnchorPane>

我在我的java源代码上获取了我的窗格并尝试添加我的图表。但该图表并未扩展以捕获窗格的整个区域。是否可以调用适合父项的方法(场景构建器将其作为每个控件的上下文菜单中的选项)。

1 个答案:

答案 0 :(得分:0)

使用HBox代替窗格。给它属性VBox.vgrow =&#34;总是&#34;。 然后创建图表,在其上调用setFillHeight(true),然后将其插入HBox。

(通过向HBox提供&#34; VBox.vgrow总是&#34;你要求它填补VBox的高度.HBox总是尽可能宽.HBox的子组件总是填充可用的宽度.HBox查找子组件的FillHeight属性,以确定它是否应该填充自己的高度。完成工作!)

编辑 - 顺便说一句,我建议不要&#34;硬编码&#34;你在FXML中的宽度/高度。除了极少数情况外,最好还是有一个可扩展的&#34;布局。此外,使用只有一个孩子的AnchorPane是没有意义的。我可以建议您查看本指南:Working With Layouts in JavaFX