How do you add labels to the options in combobox and list?

后端 未结 1 1248
旧时难觅i
旧时难觅i 2021-01-06 13:00

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 gro

1条回答
  •  迷失自我
    2021-01-06 13:47

    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 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() {
                @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 ;
    }
    

    0 讨论(0)
提交回复
热议问题