java FXML 如何设置选择框默认值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/35374822/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
FXML how to set choicebox default value
提问by user1406186
I've attempted to set the default selected item of my choicebox, however it isn't working as intended...
我试图设置我的选择框的默认选定项目,但是它没有按预期工作......
<ChoiceBox fx:id="d" value="- Select choice -">
<String fx:value="hellow"/>
</ChoiceBox>
回答by Naoghuman
This answer is answered in the question JavaFX & FXML: how do I set the default selected item in a ChoiceBox in FXML?
这个答案在问题JavaFX & FXML: how do I set the default selected item in a ChoiceBox in FXML?
For example when you want to select the second value as default value you can do following in your FXMLfile:
例如,当您想选择第二个值作为默认值时,您可以在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>
回答by Ferin Patel
You can use .setValue("");
for setting the default value.. point to note the valuename
should be present in observablearray("","","")
您可以.setValue("");
用于设置默认值.. 指出valuename
应该存在于observablearray("","","")
Example
例子
@fxml
private ChoiceBox choiceId; // this is fxml choicebox Id name given in fxml file
ObservableList<String> options = FXCollections.observableArrayList("valuename1","valuename2");
choiceId.setValue("valuename1"); // this statement shows default value
choiceId.setItems(options); // this statement adds all values in choiceBox
回答by Vivek
When we use 'value' then it will set default value for your choice box or even you can display any short message for your choice box:
当我们使用“值”时,它将为您的选择框设置默认值,甚至您可以为您的选择框显示任何短消息:
<ChoiceBox value="- Select choice -">
<items>
<FXCollections fx:factory="observableArrayList">
<String fx:value="first choice"></String>
</FXCollections>
</items>*emphasized text*
</ChoiceBox>
回答by Karl Huffaker
This question was asked many years ago but I didn't like the answers given. I was recently having the same issue and I finally realized what the problem was. Take this for example:
这个问题是多年前提出的,但我不喜欢给出的答案。我最近遇到了同样的问题,我终于意识到问题所在。以这个为例:
<ChoiceBox fx:id="cb_DBEditors" layoutX="148.0" layoutY="192.0" prefHeight="25.0" prefWidth="124.0" value="Testing">
<items>
<FXCollections fx:factory="observableArrayList">
<String fx:value="SQL Plus" />
<String fx:value="Something Else" />
</FXCollections>
</items>
</ChoiceBox>
The value you have in the default value=""
must equal one of the fx:value
strings. If it isn't then the ChoiceBox
will be blank. To recap, the default value you set must be one of the fx:value
in your <items>
list.
您在默认值中的值value=""
必须等于fx:value
字符串之一。如果不是,那么ChoiceBox
它将是空白的。总括来说,你设置的默认值必须是一个fx:value
在你的<items>
列表中。
<ChoiceBox fx:id="cb_DBEditors" layoutX="148.0" layoutY="192.0" prefHeight="25.0" prefWidth="124.0" value="SQL Plus">
<items>
<FXCollections fx:factory="observableArrayList">
<String fx:value="SQL Plus" />
<String fx:value="Something Else" />
</FXCollections>
</items>
</ChoiceBox>