Javafx 2 在javafx中的ComboBox.setConverter中使用FormatStringConverter时出现问题
我必须使用组合框来关联一系列值(键、值)。键是要存储在数据库中的值,值是要显示在组合框中的描述 例如:Javafx 2 在javafx中的ComboBox.setConverter中使用FormatStringConverter时出现问题,javafx-2,Javafx 2,我必须使用组合框来关联一系列值(键、值)。键是要存储在数据库中的值,值是要显示在组合框中的描述 例如: Key / value C1 Code one C2 Choice two C3 Choice three ... Format format = new MessageFormat("Bla bla"); cb.setConverter(new FormatStringConverter<ComboVal>(format); 例如,检索所选
Key / value
C1 Code one
C2 Choice two
C3 Choice three
...
Format format = new MessageFormat("Bla bla");
cb.setConverter(new FormatStringConverter<ComboVal>(format);
例如,检索所选值“选择二”时,我希望接收代码C2
为了在items中存储元素,我定义了ComboVal类
在定义combobox时,我被setConverter函数的定义所困扰。
编译器给了我以下错误:
错误:(1093,49)java:javafx.util.converter.FormatStringConverter类中的构造函数FormatStringConverter无法应用于给定类型;必需:java.text.Format;找到:没有参数
原因:实际参数列表和正式参数列表长度不同
代码:
class-ComboVal{
字符串[]dato={null,null};
ComboVal(字符串键、字符串值)
{
设置值(键、值);
}
ComboVal()
{
setValue(null,null);
}
字符串getValue()
{
返回dato[1];
}
字符串getKey()
{
返回dato[0];
}
void setValue(字符串键、字符串值)
{
dato[0]=键;
dato[1]=数值;
}
}
我的班级{
....
/*
参数ctrl是一个包含动态创建组合框信息的列表
*/
无效方法(列表ctrl)
{
VBox box=新的VBox();
添加(新标签(ctrl.Label));
ObservableList items=FXCollections.observableArrayList();
ComboBox cb=新ComboBox();
cb.setId(ctrl.name);
cb.设置项目(项目);
//----->>>编译器在下一行报告错误需要使用Format
参数构造Format
类。但是,您没有使用参数构造它
提供格式
,例如:
Key / value
C1 Code one
C2 Choice two
C3 Choice three
...
Format format = new MessageFormat("Bla bla");
cb.setConverter(new FormatStringConverter<ComboVal>(format);
Format Format=newmessageformat(“Bla-Bla”);
cb.setConverter(新FormatStringConverter(格式);
FormatStringConverter
已经定义了自己的toString
和fromString
方法,并将使用给定的格式来解析和显示值。我怀疑这是您想要的,因为这非常有限
因此,我认为您最好只使用一个常规的StringConverter
,并为toString
和fromString
提供自定义实现,FormatStringConverter
类需要使用Format
参数构造。但是,您构造它时没有参数
提供格式
,例如:
Key / value
C1 Code one
C2 Choice two
C3 Choice three
...
Format format = new MessageFormat("Bla bla");
cb.setConverter(new FormatStringConverter<ComboVal>(format);
Format Format=newmessageformat(“Bla-Bla”);
cb.setConverter(新FormatStringConverter(格式);
FormatStringConverter
已经定义了自己的toString
和fromString
方法,并将使用给定的格式来解析和显示值。我怀疑这是您想要的,因为这非常有限
因此,我认为您最好只使用常规的StringConverter
,并为toString
和fromString
提供自定义实现。您好@john16384。您建议的解决方案不起作用。如果您看到我的代码,则会为方法comboBox.setConverter.Hi@john16384实现自定义toString和fromString。您建议的解决方案不起作用。如果您看到我的代码,则会为方法comboBox.setConverter实现自定义toString和fromString。