从控制器访问FXML中的重复控件,而无需为每个控件提供ID

问题描述 投票:2回答:1

这就是我的FXML层次结构:

  • AnchorPane 垂直框 HBox中 ComboBox:String:[Channel Name] TextField:[开始前延迟] 按钮:[触发录制开始] HBox中 ComboBox:String:[Channel Name] TextField:[开始前延迟] 按钮:[触发录制开始] HBox中 ComboBox:String:[Channel Name] TextField:[开始前延迟] 按钮:[触发录制开始]

我想要一种方法来获取每个hbox -in my controller-内部的控件,而不必为其中的每个控件提供id。

我目前正在做的是使用每个元素的索引来得到它像这样:

@FXML
public void startRecording(MouseEvent event) {
    ObservableList<Node> curChildNodes = ((Node) event.getTarget()).getParent().getChildrenUnmodifiable();
    String               channelName   = ((ComboBox<String>) (curChildNodes.get(0))).getSelectionModel().getSelectedItem();
    long                 delay         = Long.parseLong(((TextField) curChildNodes.get(1)).getText());

    Stream stream = new Stream(channelName, delay);
    Recorder recorder = new Recorder(stream);
    recorder.startAfterDelay();
}

如果我在HBox中有更多控件,或者如果我决定改变他们在层次结构中的位置,那么我正在使用的方法会变得乏味。

有没有更好的方法呢?

java javafx javafx-8 javafx-2
1个回答
2
投票

创建一个单独的FXML文件(带有自己的控制器类),用HBoxComboBoxTextField表示Button。然后使用<fx:include>将其包含在主FXML文件中。

如果需要,您可以使用"Nested Controllers"技术在“main”控制器中引用为包含的FXML文件的每个实例创建的控制器实例。

所以你可以创建这样的东西(我称之为ChannelControls.fxml):

<HBox xmlns:fx="..." fx:controller="myapp.ChannelController">
    <ComboBox fx:id="channel"/>
    <TextField fx:id="delay"/>
    <Button text="Start" fx:id="start" onAction="startRecording"/>
</HBox>

用控制器

public class ChannelController {

    @FXML
    private ComboBox<String> channel ;
    @FXML
    private TextField delay ;
    @FXML
    private Button start ;

    @FXML
    private void startRecording(ActionEvent event) {
        String channelName = channel.getValue();
        long delayTime = Long.parseLong(delay.getText());
        // ...
    }
}

然后在你的“主”fxml中,你可以做到

<AnchorPane xmlns:fx="..." fx:controller="myapp.MainController" >
    <VBox>
        <fx:include src="ChannelControls.fxml"/>
        <fx:include src="ChannelControls.fxml"/>
        <fx:include src="ChannelControls.fxml"/>
    </VBox>
</AnchorPane>

如果您需要访问主控制器中的ChannelController实例,请将fx:ids添加到<fx:include>s:

<fx:include src="ChannelControls.fxml" fx:id="channel1" />
<fx:include src="ChannelControls.fxml" fx:id="channel2" />
<!-- etc -->

然后你可以通过将"Controller"附加到fx:id值来访问控制器:

public class MainController {

    @FXML
    private ChannelController channel1Controller ;
    @FXML
    private ChannelController channel2Controller ;

    public void initialize() {
        // do anything you need with channel1Controller, etc.
    }
}

这方面的一个小变化是实现HBox及其作为"Custom Component"的碎片。这实际上只是颠倒了FXML文件和控制器类的创建角色(因此,您不必加载FXML文件,而是自动创建控制器,而是创建控制器,自动加载FXML)。所以你可以创造

public class ChannelControls extends HBox {

    @FXML
    private ComboBox<String> channel ;
    @FXML
    private TextField delay ;
    @FXML
    private Button start ;

    public ChannelControls() {
        try {
            FXMLLoader loader = new FXMLLoader("ChannelControls.fxml");
            loader.setRoot(this);
            loader.setController(this);
            loader.load();
        } catch (IOException exc) {
            // this is pretty much fatal:
            throw new UncheckedIOException(exc);
        }
    }

    @FXML
    private void startRecording(ActionEvent event) {
        String channelName = channel.getValue();
        long delayTime = Long.parseLong(delay.getText());
        // ...
    }

    // other methods as needed
}

ChannelControls.fxml的唯一更改是对根元素:请注意,您必须删除fx:controller属性:

<fx:root type="HBox" xmlns:fx="...">
    <ComboBox fx:id="channel"/>
    <TextField fx:id="delay"/>
    <Button text="Start" fx:id="start" onAction="startRecording"/>
</fx:root>

现在您的主要fxml文件只需要

<AnchorPane xmlns:fx="..." fx:controller="myapp.MainController" >
    <VBox>
        <ChannelControls/>
        <ChannelControls/>
        <ChannelControls/>
    </VBox>
</AnchorPane>

您可以将fx:ids添加到<ChannelControls>元素,并在需要时将它们直接注入主控制器。这种方法使得在ChannelControls类中公开属性和方法并在主控制器imho中访问它们更容易一些。

© www.soinside.com 2019 - 2024. All rights reserved.