我尝试设置选择框的默认选定项目,但它没有按预期工作...
<ChoiceBox fx:id="d" value="- Select choice -">
<String fx:value="hellow"/>
</ChoiceBox>
最佳答案
这个答案在问题JavaFX & FXML: how do I set the default selected item in a ChoiceBox in FXML?中得到了回答。
例如,当您想选择第二个值作为默认值时,您可以在 FXML 文件中执行以下操作:
<?xml version="1.0" encoding="UTF-8"?>
<?import java.lang.*?>
<?import java.util.*?>
<?import javafx.collections.*?>
<?import javafx.scene.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>
<AnchorPane id="AnchorPane" prefHeight="200" prefWidth="320" xmlns:fx="http://javafx.com/fxml/1" xmlns="http://javafx.com/javafx/8" fx:controller="choicebox.defaultselection.FXMLDocumentController">
<children>
<ChoiceBox layoutX="16.0" layoutY="52.0" prefWidth="150.0" value="5 minutes">
<items>
<FXCollections fx:factory="observableArrayList">
<String fx:value="2 minutes" />
<String fx:value="5 minutes" />
<String fx:value="15 minutes" />
</FXCollections>
</items>
</ChoiceBox>
</children>
</AnchorPane>
关于java - FXML如何设置选择框默认值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35374822/