java - 选项卡 Pane 中的自定义组件不可交互

标签 java user-interface javafx javafx-2 custom-component

我创建了一个自定义组件 PlayerEditableRow,并将其放置在锚定 Pane PlayersTable 中,该 Pane 位于选项卡 PlayersTab 中,但我的可编辑行组件不是交互式的。例如,我有一个按钮和组合框,当我单击它们时它们没有响应。 PlayersTable 也不响应鼠标点击监听器。但是,当我将可编辑行放在 PlayersTab 锚定 Pane 中时,自定义组件确实有效。这是我的 GUI 的设置:

标签 Controller :

public class PlayersTab extends Tab implements IObserver {

@FXML private AnchorPane content;
@FXML private PlayersTable playerTable;

private void initView() {
    FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource("players_tab.fxml"));
    fxmlLoader.setRoot(content);
    fxmlLoader.setController(this);

    try {
        fxmlLoader.load();
    } catch (Exception e) {
        e.printStackTrace();
    }

    this.setText("Players");
    this.setContent(content);

    playerTable.initializeController(gameSettings);
}

fxml文件:

<?import dominion.application.controller.PlayersTable?>

<fx:root type="javafx.scene.layout.AnchorPane" fx:id="content" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0" xmlns:fx="http://javafx.com/fxml/1" xmlns="http://javafx.com/javafx/2.2">
  <children>
    <HBox prefHeight="100.0" prefWidth="200.0" spacing="10.0" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0">
      <children>
        <VBox fx:id="playersVBox" prefHeight="-1.0" prefWidth="-1.0" spacing="10.0" HBox.hgrow="SOMETIMES">
          <children>
            <Label text="Players" >
              <font>
                <Font size="18.0" />
              </font>
              <VBox.margin>
                <Insets />
              </VBox.margin>
            </Label>
            <PlayersTable fx:id="playerTable" />
          </children>
        </VBox>
      </children>
      <padding>
        <Insets bottom="10.0" left="10.0" right="10.0" top="10.0" />
      </padding>
    </HBox>
  </children>
</fx:root>

自定义组件:

public class PlayersTable extends AnchorPane implements IObserver {

@FXML private VBox playerTable;

private void initView() {
    FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource("players_table.fxml"));
    fxmlLoader.setRoot(this);
    fxmlLoader.setController(this);

    try {
        fxmlLoader.load();
    } catch (Exception e) {
        e.printStackTrace();
    }

    playerTable.getChildren().add(new PlayerEditableRow(PlayerType.COMPUTER, gameSettings));
}

fxml文件:

<fx:root type="javafx.scene.layout.AnchorPane" id="AnchorPane" blendMode="DARKEN" maxHeight="-1.0" maxWidth="-1.0" minHeight="-1.0" minWidth="-1.0" mouseTransparent="true" prefHeight="-1.0" prefWidth="-1.0" style="" xmlns:fx="http://javafx.com/fxml/1" xmlns="http://javafx.com/javafx/2.2">
  <children>
    <VBox fx:id="playerTable" minHeight="-1.0" minWidth="-1.0" prefHeight="-1.0" prefWidth="-1.0" style="" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0" />
  </children>
</fx:root>

我需要做什么才能让我的自定义组件具有交互性?在我使用彼此嵌套的自定义选项卡 Pane 和自定义锚定 Pane 之前,我没有遇到过这个问题。

这是我的非 Activity 组件的图像:

enter image description here

最佳答案

我想将其作为评论发布,但 Stackoverflow 说我需要很少的声誉才能做到这一点;)所以我创建了一个答案,也许它可以帮助...

你为什么不在 fxml 中添加你的 Playertables child 和 Playerstab 内容和文本?我的意思是它可能不会解决问题,但恕我直言会更好。我使用扩展 javafx 核心组件的自定义组件来做到这一点。 为什么不使用 javafx.fxml.FXMLLoader#setControllerFactory?它应该使您的代码更简洁,耦合度更低。

关于java - 选项卡 Pane 中的自定义组件不可交互,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20603811/

相关文章:

Java JTextArea 未在 GUI 中显示

Android:如何初始化 PreferenceFragment 中的所有摘要

JavaFX:如何编辑现有的 TableView 条目

java - 引用oracle中另一个模式中的表

java - 如何为 Java 应用程序调试 C++ dll

java - wsdl 属性动态变化

javafx-2 - JavaFX 中的 setDefaultCloseOperation()

java - ACTIVEMQ-发布者订阅者 Hello World 示例

java - 使 JPanel 可拖动

java - 当用户点击文本字段时可能的事件处理程序?