I read the following documentation, http://docs.oracle.com/javafx/2/ui_controls/combo-box.htm and I didn't find anything similar to my needs. I was looking for a way to group my options in a combobox. Suppose my combobox is duration. I have the following options : - Last 1 hour, last 2 hour, last 24 hours, last week , last 30 days, last 3 months, last year. I was to add a label "Short duration" and "long duration" in the combobox. The user can pick ONLY one but it will appear sorta like:
Short Duration
Last Hour
Last 2 hours
Last 24 Hours
Long Duration
Last Month
Last year
Short duration and long duration is just like header. YOU CANNOT CLICK THEM.
Thank you!
Note : I am not talking about Label ab = new Label ("Short duration");
Here is my code (I tried inserting label as an option in combobox , but you can select it)
ComboBox combobox_print_options = new ComboBox();
combobox_print_options.setPromptText("Choose the button you wish to click");
Label table = new Label("Table");
combobox_print_options.getItems().addAll(
table,
"a",
"b");
Create an item class for your combo box that declares whether or not it's a selectable item or not. (You could also add other useful API to this, such as a convenient accessor for the amount of time it represents.)
Then use a cell factory that disables the cells representing items that are not selectable:
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.ComboBox;
import javafx.scene.control.ListCell;
import javafx.scene.layout.BorderPane;
import javafx.stage.Stage;
public class ComboBoxWithSections extends Application {
@Override
public void start(Stage primaryStage) {
ComboBox<ComboBoxItem> combo = new ComboBox<>();
combo.getItems().addAll(
new ComboBoxItem("Short Duration", false),
new ComboBoxItem("Last Hour", true),
new ComboBoxItem("Last 2 hours", true),
new ComboBoxItem("Last 24 hours", true),
new ComboBoxItem("", false),
new ComboBoxItem("Long Duration", false),
new ComboBoxItem("Last Month", true),
new ComboBoxItem("Last Year", true)
);
combo.setCellFactory(listView -> new ListCell<ComboBoxItem>() {
@Override
public void updateItem(ComboBoxItem item, boolean empty) {
super.updateItem(item, empty);
if (empty) {
setText(null);
setDisable(false);
} else {
setText(item.toString());
setDisable(! item.isSelectable());
}
}
});
BorderPane root = new BorderPane(null, combo, null, null, null);
primaryStage.setScene(new Scene(root, 250, 400));
primaryStage.show();
}
public static class ComboBoxItem {
private final String name ;
private final boolean selectable ;
public ComboBoxItem(String name, boolean selectable) {
this.name = name ;
this.selectable = selectable ;
}
public String getName() {
return name ;
}
public boolean isSelectable() {
return selectable ;
}
@Override
public String toString() {
return name ;
}
}
public static void main(String[] args) {
launch(args);
}
}
Update This has been answered elsewhere but I would change the style of the headers using a CSS PseudoClass and an external CSS file:
Add
final PseudoClass header = PseudoClass.getPseudoClass("section-header");
to the start(...)
method, and change the cell factory's updateItem(...)
method as follows:
public void updateItem(ComboBoxItem item, boolean empty) {
super.updateItem(item, empty);
if (empty) {
setText(null);
setDisable(false);
pseudoClassStateChanged(header, false);
} else {
setText(item.toString());
setDisable(! item.isSelectable());
pseudoClassStateChanged(header, ! item.isSelectable());
}
}
Now attach a css file to the Scene
:
scene.getStylesheets().add("combo-box-with-sections.css");
and the css file could look like
combo-box-with-sections.css:
.combo-box-popup .list-cell {
-fx-padding: 4 0 4 20 ;
}
.combo-box-popup .list-cell:section-header {
-fx-font: italic 10pt sans-serif ;
-fx-padding: 4 0 4 5 ;
}