JavaFX包含FXML更改上层 Controller

标签 java javafx inner-classes fxml nested

我有一个小问题。 我有一个 Menu.FXML,它有一个 Controller (MenuController)。 在 Menu.FXML 内部,我包含另一个 .FXMl (Inner.FXML),它包含一个标签。 Inner.FXML 有一个 MouseClick 事件处理程序,因此当我单击 Inner.FXML 时,它会执行某些操作,我希望 Inner.FXML 鼠标监听器更改 Menu.FXML 内的文本。 我怎样才能做到这一点? 非常感谢。

[代码]

public class Main extends Application{

 @Override
 public void start(Stage stage) throws Exception {
     Parent root = FXMLLoader.load(getClass().getResource("Menu.fxml"));

     Scene scene = new Scene(root);

     stage.setScene(scene);
     stage.show();
 }
}

内部 Controller :

public class InnerController implements Initializable {
    public void buttonClick(ActionEvent event){
        System.out.println("change label!");
    }

    @Override
    public void initialize(URL url, ResourceBundle rb) {
   }    
}

菜单 FXML:

<VBox maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="nestedcontroller.MenuController">
   <children>
      <Label text="Label" />
      <fx:include source="Inner.fxml" />
   </children>
</VBox>

内部FXML。

<Pane fx:id="pane" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0" style="-fx-background-color: green;" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="nestedcontroller.InnerController">
   <children>
      <Button layoutX="271.0" layoutY="187.0" mnemonicParsing="false" onAction="#buttonClick" text="Button" />
   </children>
</Pane>

最佳答案

InnerContoller 中创建可观察的字符串属性,并从按钮的处理程序中设置它。然后从外部 Controller 观察该属性。

public class InnerController {

    private final ReadOnlyStringWrapper text = new ReadOnlyStringWrapper();

    public ReadOnlyStringProperty textProperty() { 
        return text.getReadOnlyProperty() ;
    }

    public String getText() {
        return text.get();
    }

    public void buttonClick(ActionEvent event){
        System.out.println("change label!");
        text.set("Hello world");
    }


    public void initialize() {
   }    
}

然后将 fx:id 添加到您的 fx:include 中:

<VBox maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="nestedcontroller.MenuController">
   <children>
      <Label text="Label" fx:id="label" />
      <fx:include source="Inner.fxml" fx:id="innerPane" />
   </children>
</VBox>

然后在 MenuController 中观察属性:

public class MenuController {
    @FXML
    private Label label ;
    @FXML
    private InnerController innerPaneController ;

    public void initialize() {
        innerPaneController.textProperty().addListener((obs, oldText, newText) -> 
            label.setText(newText));
        // or just label.textProperty().bind(innerPaneController.textProperty());
    }
}

请参阅FXML documentation了解详情。

关于JavaFX包含FXML更改上层 Controller ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28563285/

相关文章:

java - 为什么使用方法引用会在字节码中生成内部类?

c++ - 如何从 C++ 内部类引用封闭实例?

java - 总事务生命周期超时和最大事务超时之间的区别?

java - java.lang.System.arraycopy(Native Method) 线程 "main"java.lang.ArrayIndexOutOfBoundsException 中的异常

javafx - 一些 JavaFX 文档丢失 (NetBeans 11)

javafx TitledPane 单击事件仅适用于标题

java - Java 中的内部 block 如何访问应该超出范围的局部变量? (JVM 如何处理 Java 中的最终局部变量)

java - Windows BSOD 后什么可能导致 Eclipse 无法运行?

java - Elasticsearch 完成建议不区分大小写

java - 如何在 javafx 中将任何文本或字母转换为图像