Reputation: 41
I am looking to use a 'value' for my combo box lists similar to how you can do in HTML where you can have the label (whatever is shown in the combo box) and the value (being the returned value) like so:
List 1:
label="9am - 12pm", value="Morning"
label="12pm - 3pm", value="Afternoon"
label="3pm - 6pm", value="Evening"
So, the combo box will display the "9am - 12pm", etc. but the value returned will be "Morning".
Maybe I've been spending too much time on my web assignments that I'm going about this in a stupid way but any help would be appreciated.
Upvotes: 4
Views: 2127
Reputation: 12217
The combo box uses toString() so you just need to output there what you want to have displayed:
class TimeSpan
{
String label,value;
public TimeSpan(String label, String value)
{
this.label = label;
this.value = value;
}
@Override public String toString() {return label;}
}
If for some reason you cannot do that, e.g. it's not your own class or you need toString() to work differently for some other case, you can encapsulate it as follows, assuming the class above as your base class:
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.: This is the same as AxelH's approach, but I think you don't need the StringConverter.
Upvotes: -1
Reputation: 209724
Create a class to encapsulate the entity that you want to be displayed in the combo box:
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 ;
}
}
Now you can make a ComboBox
that displays these:
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"));
You can customize the display by defining a list cell:
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())));
}
}
}
and then
timeOfDayCombo.setCellFactory(lv -> new TimeOfDayListCell());
timeOfDayCombo.setButtonCell(new TimeOfDayListCell());
Now calling timeOfDayCombo.getValue()
will return the TimeOfDay
instance, from which you can call any method you need (such as getShortDescription()
).
Upvotes: 6
Reputation: 14582
What you can do is use a custome class that will hold those value. Use that class to create an ObservableList
to create this Combobox.
public class MyHolder{
private int id;
private String label;
public MyHolder(int id, String label){
//...
}
//GETTER (AND SETTER IF NEEDED)
}
Then, using a StringConverter
using this same class as type, you define the method toString
to return the String you want.
public class MyConverterHolder extends StringConverter<MyHolder>{
public String toString(MyHolder holder){
return holder.getName();
}
//...
}
You set the converter to the combobox, and you are set.
Then to get the selectedValue, get the selectionModel, and get the item with getModelItem
. Here you will get an instance of the specified type, just get the value you want.
This is a solution from documentation only, I didn't write an example to test it but this seems to be correct from what I have read. If this is not clear or not working, I will write an example (I could use a working example to get this started ;) ).
Upvotes: 1