java - 如何从 JavaFX2 的父 Controller 获取 Controller 的特定实例?

标签 java javafx controller javafx-2 fxml

我有 2 个 fxml 布局,其中一个包含另一个。我尝试从父级 Controller 更新内部元素的内容,i。 e.当按下父 Controller 上的按钮时,会更改 imageview 中的图像。

我使用的方法,建议here 。问题是当我调用假设更改 imageView 内图像的方法时(questionController.setPdfPageImage(++currentPage);),什么也没有发生。我尝试了一些调试,我相信这是因为有两个完全不同的 Controller 实例:一个从运行时调用,另一个默认从 fxml 调用。请指出正确的解决方案。

ma​​in.fxml 的一部分

<BorderPane 
    fx:id="mainContainer" 
    xmlns="http://javafx.com/javafx/8" 
    xmlns:fx="http://javafx.com/fxml/1"
    fx:controller="com.package.name.MainController">
    <center>
        <Pane fx:id="center">
            <fx:include source="question.fxml"/>
        </Pane>
    </center>
    <bottom>
        <VBox fx:id="bottom"
              BorderPane.alignment="CENTER">
            <children>
                <Button fx:id="next" text="Next"/>
            </children>
        </VBox>
    </bottom>
</BorderPane>

question.fxml 的一部分

<SplitPane fx:id="content" 
    dividerPositions="0.5" 
    xmlns="http://javafx.com/javafx/8" 
    xmlns:fx="http://javafx.com/fxml/1"
    fx:controller="com.package.name.QuestionController">
    <items>
        <AnchorPane fx:id="questionContainer">
            <children>
                <ImageView fx:id="questionView" pickOnBounds="true" preserveRatio="true"/>
            </children>
        </AnchorPane>
    </items>
</SplitPane>

QuestionController.java 的一部分

public void setPdfPageImage(int pageNum) {
//      InputStream is = QuestionController.class.getResourceAsStream(currentPdf);
        InputStream is = this.getClass().getResourceAsStream(currentPdf);
        Image convertedImage;
        try {
            PDDocument document = PDDocument.load(is);
            List<PDPage> list = document.getDocumentCatalog().getAllPages();
            PDPage page = list.get(pageNum);
            BufferedImage image = page.convertToImage(BufferedImage.TYPE_INT_RGB, 128);
            convertedImage = SwingFXUtils.toFXImage(image, null);
            document.close();
            questionView.setImage(convertedImage);    
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

MainController.java 的一部分

    public class MainController implements Initializable {
    QuestionController questionController;
    @FXML //  fx:id="next"
    private Button next; // Value injected by FXMLLoader
    @Override
    public void initialize(URL location, ResourceBundle resources) {
        FXMLLoader loader = new FXMLLoader(getClass().getResource("/question.fxml"));
        try {
            loader.load();
        } catch (IOException e1) {
            e1.printStackTrace();
        }
        questionController = (QuestionController) loader.getController();
        next.setOnAction(event -> {
            //TODO remove hardcoded value 49
            if (currentPage < 49)
                questionController.setPdfPageImage(++currentPage);
            else {
                Alert alert = new Alert(Alert.AlertType.INFORMATION);
                alert.setTitle("Quiz finished");
                alert.setHeaderText(null);
                alert.setContentText("This was the last question. Thank you!");
                alert.showAndWait();
            }
        });
    }
}

最佳答案

要注入(inject)包含的 fxml 的 Controller ,请添加 fx:id属性为fx:include标签:

<fx:include fx:id="question" source="question.fxml"/>

这会将 Controller 注入(inject)到名为 <fx:id>Controller 的字段中,即questionController在这种情况下(如果这样的字段对加载程序可见)。

(同时从 initialize 方法中删除加载程序部分)。

关于java - 如何从 JavaFX2 的父 Controller 获取 Controller 的特定实例?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34781051/

相关文章:

java:扫描仪在使用useDelimiter时要求额外的值

java - 为什么是 `sun.jvm.hotspot.debugger.DebuggerException: cannot open binary file` ?

css - JavaFX CSS 主题

node.js - ExpressJS 在路由内调用路由

javascript - 渲染多个 View / Controller 对

java - 如何将字符串乘以整数?

java - 如何使用 Groovy 或其他一些 JVM 语言对 Java 对象进行 Duck 类型

java - 当我使用选项卡时 getSelectionModel 无法打开

绘制图表时 JavaFX WeakReference 内存泄漏

java - 如何检查获取参数是否已设置?