JavaFX ChoiceBox add separator with type safety

javafx separator
javafx separator thickness
javafx choicebox fxml
javafx choicebox listener
javafx separator padding
javafx separator fxml
javafx choicebox scene builder
javafx combobox cell factory

I'm looking to add a separator into a choice box and still retain the type safety.

On all of the examples I've seen, they just do the following:

ChoiceBox<Object> cb =  new ChoiceBox<>();
cb.getItems().addAll("one", "two", new Separator(), "fadfadfasd", "afdafdsfas");

Has anyone come up with a solution to be able to add separators and still retain type safety?

I would expect that if I wanted to add separators, I should be able do something along the following:

ChoiceBox<T> cb = new ChoiceBox<T>();
cb.getSeparators().add(1, new Separator()); // 1 is the index of where the separator should be

I shouldn't have to sacrifice type safety just to add separators.

Using JavaFX UI Controls: Separator, In Example 15-2, the separator is added to the vertical box by using a dedicated method add(index, node) . You can use this approach in your application to� The following are top voted examples for showing how to use javafx.collections.ListChangeListener.These examples are extracted from open source projects. You can vote up the examples you like and your votes will be used in our system to generate more good examples.

Well you can work around it by creating an interface and then subclassing Separator to implement this interface:

import javafx.application.Application;
import javafx.beans.binding.Bindings;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.control.ChoiceBox;
import javafx.scene.control.Separator;
import javafx.scene.layout.GridPane;
import javafx.scene.text.Text;
import javafx.stage.Stage;

public class ChoiceBoxIsSafe extends Application {

    interface FruitInterface { }

    static public class Fruit implements FruitInterface {
        private StringProperty name = new SimpleStringProperty();
        Fruit(String name) {

        public StringProperty nameProperty() {
            return name;

        public String toString() {
            return name.get();

    static public class FruitySeparator extends Separator implements FruitInterface { }

    public void start(Stage primaryStage) throws Exception {
        GridPane grid = new GridPane();
        grid.setHgap(10); grid.setVgap(10); grid.setPadding(new Insets(10));

        ChoiceBox<FruitInterface> cb = new ChoiceBox<>();
        cb.getItems().addAll(new Fruit("Apple"), new Fruit("Orange"), new FruitySeparator(), new Fruit("Peach"));

        Text text = new Text("");

        ReadOnlyObjectProperty<FruitInterface> selected = cb.getSelectionModel().selectedItemProperty();
        text.textProperty().bind(, "name"));

        grid.add(cb, 0, 0);
        grid.add(text, 1, 0);

        Scene scene = new Scene(grid);
        primaryStage.setScene(scene);       ;

    public static void main(String[] args) {

but that is hardly an "elegant" solution and cannot be done in all cases (e.g. ChoiceBox<String>).

From the implementation of ChoiceBox it certainly looks like it wasn't a good idea to treat Separators like items in the ChoiceBox :-(.

Using JavaFX UI Controls: Choice Box, Use the ChoiceBox class to add choice boxes to your JavaFX applications. Its simple Example 7-2 Choice Box with Text Elements and a Separator ChoiceBox� JavaFX classes support polymorphism and can be subclassed.A JavaFX class can inherit behavior from a single base class and from one or more of a special type of class called a mixin.A JavaFX class can be abstract and can implement Java interfaces, but there is no such thing as a JavaFX interface.


There is a MUCH easier way to do this using code (there are easy ways to do it using FXML too, doing it in code offers more flexibility).

You simply create an ObservableList, then populate it with your items, including the separator then assign that list to the ChoiceBox like this:

private void fillChoiceBox(ChoiceBox choiceBox) {

    ObservableList items = FXCollections.observableArrayList();

    items.add(new Separator());


JavaFX Documentation Project, Authors are encouraged to add their name to the contributors list in the The ChoiceBox control is a list of values from which the user makes a selection. The TableView and TableColumn use generics in their declarations. The UI also features a Separator which divides the top part of the screen with� Java Type Casting. Type casting is when you assign a value of one primitive data type to another type. In Java, there are two types of casting: Widening Casting (automatically) - converting a smaller type to a larger type size byte-> short-> char-> int-> long-> float-> double

javafx, JavaFX ChoiceBox add separator with type safety. I'm looking to add a separator into a choice box and still retain the type safety. On all of the examples I've� Add features such as binding the UI to properties in a model and change listeners that reduce the need for setter methods, and you have a combination that will help

JavaFX ChoiceBox, JavaFX ChoiceBox. Creating a ChoiceBox; Adding Choices to a ChoiceBox; Adding a ChoiceBox to the Scene Graph; Reading the Selected� [JDK-8098105] - Add GL_FRAGMENT_PRECISION_HIGH check to existing 2D shaders before setting precision to highp [JDK-8098123] - TableView: support update of items that are equal, part 2 [JDK-8098134] - Add new property to file [JDK-8098146] - support for Android Fragment

Release Notes - Java Bug System, CheckContext interface; [JDK-7177385] - Add attribution support for lambda expressions [JDK-8017325] - Cleanup of the javadoc <code> tag in java. security.cert ClassPath builds a path using File.separator instead of File. and motif properties; [JDK-7169332] - add deploy and javafx version mime type to npjp2� Enum is a keyword in java and on more detail term Java Enum is type like class and interfaces and can be used to define a set of Enum constants. Enum constants are implicitly static and final and you cannot change their value once created. Enum in Java provides type-safety and can be used inside switch statement like int variables.

  • interesting idea :-) Musing: which party would be responsible for keeping the index in sync on modifications of the data? Probably the skin (that's where separators are mapped to SeparatorMenuItems)? And we wouldn't need a full-fledged separator, just a marker (the separator isn't used anyway)
  • Perhaps you could use a ComboBox rather than a ChoiceBox. Create a custom type, e.g. SeparableElement<T>, which holds the elements of your type T and knows where the separators are supposed to go, then define a custom cell factory which renders the elements and non-selectable separators as appropriate (probably quite tricky to implement well).
  • But this doesn't do what the question asks, which is to add the Separator to the list choices with type safety. To have type safety you need to supply the concrete types via type parameters when specifying type definitions.
  • I do agree though that this implementation is simple and, for most people, likely sufficient. The effort involved to create a generic type safe solution to this problem would likely outweigh the benefit of doing so.