JavaFX如何处理titledpane expandproperty?

时间:2015-12-17 14:27:52

标签: javafx

有人给我一个暗示这个问题吗? 我正在测试一些应用程序,它可以在我打开TitlePane时立即将文本写入TextArea。当我点击titlepane2的标题时,我编写了我的应用程序,一些文本应该写在textArea2中,并且它工作正常。但问题是当我点击titlepane1中的完成按钮时,如何写一些文字?如您所见,如果单击完成按钮,将扩展titlepane2,并且应在textArea2中写入一些文本。 我不知道如何解决这个问题。请帮助我

import java.net.URL;
import java.util.ResourceBundle;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.TextArea;
import javafx.scene.control.TitledPane;

/**
 * FXML Controller class
 *
 * @author James
 */
public class TitlePaneTestController implements Initializable {

    @FXML
    private TitledPane titlePane1;
    @FXML
    private TitledPane titlePane2;
    @FXML
    private TextArea textArea1;
    @FXML
    private TextArea textArea2;



    /**
     * Initializes the controller class.
     */
    @Override
    public void initialize(URL url, ResourceBundle rb) {
        // TODO
    }

    @FXML
    private void handleTitlePane1Clicked() {
        textArea1.appendText("This is TitlePane 1");
    }

    @FXML
    private void handleTitle1Finished() {
        titlePane1.setExpanded(false);
        titlePane2.setExpanded(true);
    }

    @FXML
    private void handleTitlePane2Clicked() {
        textArea2.appendText("This is TitlePane 1");
    }

}

以下是FXML文件

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

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


<AnchorPane id="AnchorPane" prefHeight="400.0" prefWidth="600.0" xmlns:fx="http://javafx.com/fxml/1" xmlns="http://javafx.com/javafx/8" fx:controller="smallframework.TitlePaneTestController">
   <children>
      <Accordion layoutX="107.0" layoutY="101.0" prefHeight="400.0" prefWidth="600.0" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0">
        <panes>
          <TitledPane fx:id="titlePane1" animated="false" text="Step 1">
            <content>
              <AnchorPane minHeight="0.0" minWidth="0.0" prefHeight="180.0" prefWidth="200.0">
                     <children>
                        <VBox prefHeight="353.0" prefWidth="598.0" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0">
                           <children>
                              <TextArea fx:id="textArea1" prefHeight="200.0" prefWidth="200.0" />
                              <Button mnemonicParsing="false" onAction="#handleTitle1Finished" text="Finish" />
                           </children>
                        </VBox>
                     </children>
                  </AnchorPane>
            </content>
          </TitledPane>
          <TitledPane fx:id="titlePane2" animated="false" text="Step 2">
            <content>
              <AnchorPane minHeight="0.0" minWidth="0.0" prefHeight="180.0" prefWidth="200.0">
                     <children>
                        <VBox prefHeight="353.0" prefWidth="598.0">
                           <children>
                              <TextArea fx:id="textArea2" prefHeight="200.0" prefWidth="200.0" />
                              <Button mnemonicParsing="false" text="Finish" />
                           </children>
                        </VBox>
                     </children>
                  </AnchorPane>
            </content>
          </TitledPane>
        </panes>
      </Accordion>
   </children>
</AnchorPane>

1 个答案:

答案 0 :(得分:0)

使用每个标题窗格的expandedProperty注册一个监听器:

@Override
public void initialize(URL url, ResourceBundle rb) {
    titlePane1.expandedProperty().addListener((obs, wasExpanded, isNowExpanded) -> {
        if (isNowExpanded) {
            textArea1.appendText("Titled Pane 1 selected");
        }
    }
    titlePane2.expandedProperty().addListener((obs, wasExpanded, isNowExpanded) -> {
        if (isNowExpanded) {
            textArea2.appendText("Titled Pane 2 selected");
        }
    }
}