是否可以将手风琴的大小动态绑定到JavaFX-8中扩展的标题窗格的大小?

时间:2016-07-16 10:25:37

标签: size javafx-8 accordion changelistener

如标题所示,我正在尝试将Accordion的大小动态绑定到展开的TitledPane的大小。

我尝试使用以下代码在包含ChangeListener的UI的控制器初始化方法中向expandedPaneProperty accordion添加Accordion:< / p>

private double prefHeight, prefWidth;

@Override
public void initialize(URL location, ResourceBundle resources) {
    prefHeight = acc.getPrefHeight(); //double variables of controller
    prefWidth = acc.getPrefWidth();
    acc.expandedPaneProperty().addListener(new ChangeListener<Object>(){

        @Override
        public void changed(ObservableValue<? extends Object> observable, Object oldValue, Object newValue) {
            TitledPane pane = acc.getExpandedPane();
            if(pane != null){
                acc.setPrefHeight(prefHeight + pane.getPrefHeight());
                acc.setPrefWidth(prefWidth + pane.getPrefWidth());
            }

        }
    });
}

问题是,此代码在第3行导致NullPointerException,这可能表示此时可能未对该属性进行实例化。 更新:有关NullPointerException的问题已解决,我没有正确初始化Accordion。 但是,代码仍无效。

相应的FXML:

<Accordion fx:id="acc" xmlns="http://javafx.com/javafx/8.0.60" xmlns:fx="http://javafx.com/fxml/1" fx:controller="application.SettingsController">
  <panes>
    <TitledPane alignment="TOP_LEFT" animated="false" text="Alarm sound">
         <tooltip>
            <Tooltip text="Enables to choose an alarm sound." />
         </tooltip>
         <content>
            <VBox alignment="TOP_CENTER" maxHeight="1.7976931348623157E308" maxWidth="1.7976931348623157E308" styleClass="box">
               <children>
                  <HBox alignment="TOP_CENTER" styleClass="hbox">
                     <children>
                        <Button mnemonicParsing="false" text="Reset to default" />
                        <Button mnemonicParsing="false" onAction="#saveAndExitOnClick" text="Save and exit" />
                     </children>
                  </HBox>
                  <ListView maxHeight="1.7976931348623157E308" maxWidth="1.7976931348623157E308" VBox.vgrow="ALWAYS" />
               </children>
            </VBox>
         </content></TitledPane>
    <TitledPane animated="false" text="Style">
         <tooltip>
            <Tooltip text="Enables to choose a style for the program." />
         </tooltip>
         <content>
            <VBox maxHeight="1.7976931348623157E308" maxWidth="1.7976931348623157E308" styleClass="box">
               <children>
                  <HBox alignment="TOP_CENTER" styleClass="h-box">
                     <children>
                        <Button mnemonicParsing="false" text="Reset to default" />
                        <Button mnemonicParsing="false" onAction="#saveAndExitOnClick" text="Save and exit" />
                     </children>
                  </HBox>
                  <ListView maxHeight="1.7976931348623157E308" maxWidth="1.7976931348623157E308" />
               </children>
            </VBox>
         </content></TitledPane>
      <TitledPane animated="false" text="Language">
         <tooltip>
            <Tooltip text="Enables to choose a style for the program." />
         </tooltip>
         <content>
            <VBox maxHeight="1.7976931348623157E308" maxWidth="1.7976931348623157E308" styleClass="box">
               <children>
                  <HBox alignment="TOP_CENTER">
                     <children>
                        <Button mnemonicParsing="false" text="Reset to default" />
                        <Button mnemonicParsing="false" onAction="#saveAndExitOnClick" text="Save and exit" />
                     </children>
                  </HBox>
                  <ListView maxHeight="1.7976931348623157E308" maxWidth="1.7976931348623157E308" />
               </children>
            </VBox>
         </content>
      </TitledPane>
      <TitledPane maxHeight="1.7976931348623157E308" maxWidth="1.7976931348623157E308" styleClass="box" text="Other settings">
         <content>
            <ToolBar maxHeight="1.7976931348623157E308" maxWidth="1.7976931348623157E308" orientation="VERTICAL" styleClass="box">
              <items>
                <Button contentDisplay="CENTER" maxWidth="1.7976931348623157E308" mnemonicParsing="false" text="Recenter Window" />
                  <Button maxWidth="1.7976931348623157E308" mnemonicParsing="false" text="Reset program to default settings" />
                  <Button maxWidth="1.7976931348623157E308" mnemonicParsing="false" text="Load custom alarm sound" />
                  <Button maxWidth="1.7976931348623157E308" mnemonicParsing="false" text="Apply custom style" />
              </items>
            </ToolBar>
         </content>
      </TitledPane>
      <TitledPane maxHeight="1.7976931348623157E308" prefWidth="200.0" text="About">
         <content>
            <TextArea editable="false" maxHeight="1.7976931348623157E308" maxWidth="1.7976931348623157E308" text="Timer Program, written by QBW.&#10;Version: &#10;&#10;Fonts: &#10;Font Awesome, License:&#10;&#10;Alarm sounds:&#10;&#10;&#10;Acknowledgement:&#10;·Hans-Peter Habelitz for &quot;Programmieren lernen mit Java&quot;&#10;&#10;·Christian Ullenboom for &quot;Java ist auch eine Insel&quot; and &quot;Java SE8 Standardbibliothek&quot;&#10;&#10;·Anton Epple for &quot;JavaFX 8 - Grundlagen und fortgeschrittene Techniken&quot;&#10;&#10;·The stackoverflow.com community&#10;&#10;· Creators of alarm sound files:&#10;     ·&#10;     ·&#10;     ·&#10;     ·&#10;     ·&#10;· Creators of Font awesome&#10;" wrapText="true" />
         </content>
         <tooltip>
            <Tooltip text="Contains information about the program, its creator and license information" />
         </tooltip>
      </TitledPane>
  </panes>
</Accordion>

如何解决这个问题?

0 个答案:

没有答案