我希望使用'值'对于我的组合框列表类似于您可以在HTML中执行的操作,您可以在其中获得标签(组合框中显示的内容)和值(返回值),如下所示:
清单1:
label="9am - 12pm", value="Morning"
label="12pm - 3pm", value="Afternoon"
label="3pm - 6pm", value="Evening"
因此,组合框将显示" 9am - 12 pm"等等,但返回的值将是" Morning"。
也许我已经花了太多时间在我的网络作业上,以愚蠢的方式解决这个问题,但任何帮助都会受到赞赏。
答案 0 :(得分:3)
创建一个类来封装要在组合框中显示的实体:
@Configuration
@EnableWebMvc
public class WebConfig extends WebMvcConfigurerAdapter {
@Override
public void addInterceptors(InterceptorRegistry registry) {
registry.addInterceptor(new ThemeInterceptor()).addPathPatterns("/**").excludePathPatterns("/ajax/**");
}
}
现在您可以制作显示以下内容的import java.time.LocalTime ;
// maybe an enum would be good here too
public class TimeOfDay {
private final LocalTime startTime ;
private final LocalTime endTime ;
private final String shortDescription ;
public TimeOfDay(LocalTime startTime, LocalTime endTime, String description) {
this.startTime = startTime ;
this.endTime = endTime ;
this.shortDescription = description ;
}
public LocalTime getStartTime() {
return startTime ;
}
public LocalTime getEndTime() {
return endTime ;
}
public String getShortDescription() {
return shortDescription ;
}
}
:
ComboBox
您可以通过定义列表单元格来自定义显示:
ComboBox<TimeOfDay> timeOfDayCombo = new ComboBox<>();
timeOfDayCombo.getItems().addAll(
new TimeOfDay(LocalTime.of(9,0), LocalTime.of(12,0), "Morning"),
new TimeOfDay(LocalTime.of(12,0), LocalTime.of(15,0), "Afternoon"),
new TimeOfDay(LocalTime.of(15,0), LocalTime.of(18,0), "Evening"));
然后
import java.time.LocalTime ;
import java.time.format.DateTimeFormatter ;
public class TimeOfDayListCell extends ListCell<TimeOfDay> {
private final DateTimeFormatter formatter = DateTimeFormatter.ofPattern("ha");
@Override
protected void updateItem(TimeOfDay timeOfDay, boolean empty) {
super.updateItem(timeOfDay, empty) ;
if (empty) {
setText(null);
} else {
setText(String.format("%s - %s",
formatter.format(timeOfDay.getStartTime()),
formatter.format(timeOfDay.getEndTime())));
}
}
}
现在调用timeOfDayCombo.setCellFactory(lv -> new TimeOfDayListCell());
timeOfDayCombo.setButtonCell(new TimeOfDayListCell());
会返回timeOfDayCombo.getValue()
个实例,您可以从中调用所需的任何方法(例如TimeOfDay
)。
答案 1 :(得分:0)
你能做的就是使用能够保持这些价值的客户类。使用该类创建ObservableList
以创建此Combobox。
public class MyHolder{
private int id;
private String label;
public MyHolder(int id, String label){
//...
}
//GETTER (AND SETTER IF NEEDED)
}
然后,使用同一类作为类型的StringConverter
,定义方法toString
以返回所需的字符串。
public class MyConverterHolder extends StringConverter<MyHolder>{
public String toString(MyHolder holder){
return holder.getName();
}
//...
}
您将转换器设置为组合框,然后设置。
然后,要获取selectedValue,请获取selectionModel,然后使用getModelItem
获取该项目。在这里,您将获得指定类型的实例,只需获取所需的值。
这是一个仅来自文档的解决方案,我没有编写一个示例来测试它,但这似乎与我所读到的一致。如果这不清楚或不起作用,我会写一个例子(我可以使用一个工作示例来启动它;))。
答案 2 :(得分:-1)
组合框使用toString(),因此您只需要在此处输出要显示的内容即可。
class TimeSpan
{
String label,value;
public TimeSpan(String label, String value)
{
this.label = label;
this.value = value;
}
@Override public String toString() {return label;}
}
如果由于某种原因您不能这样做,例如它不是您自己的类,或者您需要toString()在某些其他情况下可以以不同的方式工作,因此可以将其封装为以下内容,并假定上面的类为您的基类:
class TimeSpanFormatter
{
TimeSpan timeSpan;
public TimeSpanFormatter(TimeSpan timeSpan)
{
this.timeSpan = timeSpan;
}
@Override public String toString() {return "Whatever you want to output with "+timeSpan.label;}
}
P.S .:与AxelH的方法相同,但是我认为您不需要StringConverter。