【问题标题】:Custom Component in Tab Pane not Interactive选项卡窗格中的自定义组件不交互
【发布时间】:2014-01-03 10:10:44
【问题描述】:

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

标签控制器:

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>

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

这是我的非活动组件的图像:

【问题讨论】:

  • 我也有同样的问题。你找到解决办法了吗?
  • 不,在我找到修复之前,我只是删除了额外的锚窗格并将所有内容放在选项卡视图中。

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


【解决方案1】:

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

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

【讨论】:

    猜你喜欢
    • 2021-06-29
    • 2017-10-14
    • 1970-01-01
    • 2020-08-06
    • 2018-12-23
    • 2019-08-13
    • 1970-01-01
    • 1970-01-01
    • 2015-01-17
    相关资源
    最近更新 更多