3

Is it possible to add a title/label to a CheckComboBox? I am thinking about a default item that is not selectable. I am using an inline form that does not have labels for input fields. I wanted to use an inlined title for the CheckComboBox.

user3111525
  • 5,013
  • 9
  • 39
  • 64

1 Answers1

0

There's no functionality in the API for this as far as I know.

But I implemented this myself for ComboBox a while ago, I don't think you will have any trouble tweaking it to fit your needs. I'm posting an MCVE of this below. At least you get the idea.

Edit: It seems like CheckComboBox doesn't have setCellFactory method. So maybe my implementation doesn't help you that much. Maybe one alternative would be to implement your own CheckComboBox from the ComboBox in the normal JavaFX.

MCVE:

import javafx.application.Application;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.scene.Scene;
import javafx.scene.control.ComboBox;
import javafx.scene.control.ListCell;
import javafx.scene.layout.HBox;
import javafx.stage.Stage;

public class MCVE extends Application {

    @Override
    public void start(Stage stage) {
        HBox box = new HBox();

        ComboBox<ComboBoxItem> cb = new ComboBox<ComboBoxItem>();

        cb.setCellFactory(e -> 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());

                    // If item is a header we disable it.
                    setDisable(!item.isHeader());

                    // If item is a header we add a style to it so it looks like a header.
                    if (item.isHeader()) {
                            setStyle("-fx-font-weight: bold;");
                    } else {
                        setStyle("-fx-font-weight: normal;");
                    }
                }
            }
        });

        ObservableList<ComboBoxItem> items = FXCollections.observableArrayList(new ComboBoxItem("Header", true),
                new ComboBoxItem("Option", false));

        cb.getItems().addAll(items);

        box.getChildren().add(cb);

        stage.setScene(new Scene(box));
        stage.show();
    }

    public static void main(String[] args) {
        launch();
    }

    /**
     * A wrapping class for a String that also has a boolean that indicates
     * whether this item should be disabled or not.
     * 
     * @author Jonatan Stenbacka
     */
    public class ComboBoxItem implements Comparable<ComboBoxItem> {

        private String text;
        private boolean isHeader = false;

        public ComboBoxItem(String text, boolean isHeader) {

            this.text = text;
            this.isHeader = isHeader;
        }

        public ComboBoxItem(String text) {
            this.text = text;
        }

        public void setText(String text) {
            this.text = text;
        }

        public String getText() {
            return text;
        }

        public boolean isHeader() {
            return isHeader;
        }

        @Override
        public String toString() {
            return text;
        }

        @Override
        public boolean equals(Object obj) {
            if (obj == this) {
                return true;
            } else if (obj instanceof ComboBoxItem) {
                ComboBoxItem item = (ComboBoxItem) obj;
                if (text.equals(item.getText())) {
                    return true;
                }
            } else if (obj instanceof String) {
                String item = (String) obj;
                if (text.equals(item)) {
                    return true;
                }
            }

            return false;
        }

        @Override
        public int compareTo(ComboBoxItem o) {
            return getText().compareTo(o.getText());
        }

        @Override
        public int hashCode() {
            return text.hashCode();
        }
    }
}
Jonatan Stenbacka
  • 1,824
  • 2
  • 23
  • 48