Hbox 有子 Buttons ,如何抓取并添加监听器?
Hbox has child Buttons , how to grab and addlisteners to them?
首先是 FXML,一个简单的 Hbox,里面有按钮:
<HBox fx:id="hboxOfCategories" alignment="CENTER_LEFT" spacing="10.0">
<children>
<Button maxHeight="1.7976931348623157E308" mnemonicParsing="false" prefHeight="57.0" prefWidth="82.0" text="Boissons" />
<Button layoutX="10.0" layoutY="10.0" maxHeight="1.7976931348623157E308" mnemonicParsing="false" prefHeight="57.0" prefWidth="82.0" text="Burger" />
<Button layoutX="102.0" layoutY="10.0" maxHeight="1.7976931348623157E308" mnemonicParsing="false" prefHeight="57.0" prefWidth="82.0" text="Tacos" />
<Button layoutX="378.0" layoutY="10.0" maxHeight="1.7976931348623157E308" mnemonicParsing="false" prefHeight="57.0" prefWidth="82.0" text="Pizza" />
<Button layoutX="194.0" layoutY="10.0" maxHeight="1.7976931348623157E308" mnemonicParsing="false" prefHeight="57.0" prefWidth="82.0" text="Baguette Farcie" textAlignment="CENTER" wrapText="true" />
<Button layoutX="286.0" layoutY="10.0" maxHeight="1.7976931348623157E308" mnemonicParsing="false" prefHeight="57.0" prefWidth="82.0" text="Souflee" />
</children>
</HBox>
我把他的内容保存在一个 Observable 列表中,它必须是 Node 类型,因为 .getChildren() 方法 returns 是 Node 类型的东西:
fxml 控制器代码:
@FXML
private void initialize(){
ObservableList<Node> hboxButtons = hboxOfCategories.getChildren();
}
我如何抓住这些按钮并向它们添加一个在单击按钮时触发的侦听器?
像这样:
hboxofCategories.getchildren().addlistener(e -> {
doEpicStuff();
});
这应该可以解决问题:
for(Node button : hboxOfCategories.getChildren())
{
((Button)button).setOnAction(a -> {
System.out.println("Pressed : "+ ((Button)button).getText());
});
}
输出:
编辑:
获取索引的方法如下:
for(int i=0;i<hboxOfCategories.getChildren().size();i++)
{
Button button=(Button)hboxOfCategories.getChildren().get(i);
int index=i;
button.setOnAction(a->
{
System.out.println("Name : "+button.getText()+" | Index : "+index);
});
}
首先是 FXML,一个简单的 Hbox,里面有按钮:
<HBox fx:id="hboxOfCategories" alignment="CENTER_LEFT" spacing="10.0">
<children>
<Button maxHeight="1.7976931348623157E308" mnemonicParsing="false" prefHeight="57.0" prefWidth="82.0" text="Boissons" />
<Button layoutX="10.0" layoutY="10.0" maxHeight="1.7976931348623157E308" mnemonicParsing="false" prefHeight="57.0" prefWidth="82.0" text="Burger" />
<Button layoutX="102.0" layoutY="10.0" maxHeight="1.7976931348623157E308" mnemonicParsing="false" prefHeight="57.0" prefWidth="82.0" text="Tacos" />
<Button layoutX="378.0" layoutY="10.0" maxHeight="1.7976931348623157E308" mnemonicParsing="false" prefHeight="57.0" prefWidth="82.0" text="Pizza" />
<Button layoutX="194.0" layoutY="10.0" maxHeight="1.7976931348623157E308" mnemonicParsing="false" prefHeight="57.0" prefWidth="82.0" text="Baguette Farcie" textAlignment="CENTER" wrapText="true" />
<Button layoutX="286.0" layoutY="10.0" maxHeight="1.7976931348623157E308" mnemonicParsing="false" prefHeight="57.0" prefWidth="82.0" text="Souflee" />
</children>
</HBox>
我把他的内容保存在一个 Observable 列表中,它必须是 Node 类型,因为 .getChildren() 方法 returns 是 Node 类型的东西:
fxml 控制器代码:
@FXML
private void initialize(){
ObservableList<Node> hboxButtons = hboxOfCategories.getChildren();
}
我如何抓住这些按钮并向它们添加一个在单击按钮时触发的侦听器? 像这样:
hboxofCategories.getchildren().addlistener(e -> {
doEpicStuff();
});
这应该可以解决问题:
for(Node button : hboxOfCategories.getChildren())
{
((Button)button).setOnAction(a -> {
System.out.println("Pressed : "+ ((Button)button).getText());
});
}
输出:
编辑:
获取索引的方法如下:
for(int i=0;i<hboxOfCategories.getChildren().size();i++)
{
Button button=(Button)hboxOfCategories.getChildren().get(i);
int index=i;
button.setOnAction(a->
{
System.out.println("Name : "+button.getText()+" | Index : "+index);
});
}