【问题标题】:How can I make combobox heading more apparent?如何使组合框标题更明显?
【发布时间】:2015-01-08 15:11:57
【问题描述】:

在我之前的问题中,How do you add labels to the options in combobox and list?

我曾询问如何将标题添加到我的组合框!答案是完美的,但我无法区分我的标题和我的选项。 是否有可能缩进任何可以选择的东西?或者让我的标题加粗?我的代码几乎是我上一个问题的最佳答案。这是……

所有标题均不可选择。

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);
}
}

【问题讨论】:

    标签: java combobox javafx


    【解决方案1】:

    您需要了解细胞工厂。 updateItem 方法是绘制单元格的地方。

            super.updateItem(item, empty);//does important things in super
            if (empty) {                  //what to do if cell is empty   
                setText(null);
                setDisable(false);        //reset the disabled state 
            } else {
                setText(item.toString()); //calls toString in your custom class
                setDisable(! item.isSelectable()); //what stops you from selecting
                setTextFill(item.isSelectable()
                        ?Color.BLUE:Color.BLACK); //changing the color
            }
    

    您可以向自定义类添加颜色或字体,然后使用它代替 isSelectable() 来确定颜色。

    您也可以使用 CSS 为禁用的单元格设置不同的样式,但这可能不太灵活。

    【讨论】:

    • 与使用 css 对禁用单元格进行不同样式设置的想法类似,我在测试 previous question 的解决方案时尝试使用 css 伪类,效果非常好。
    • @James_D 感谢所有的伪类要点顺便说一句,这就是我学习它们的地方。在这种情况下,我认为他可以使用.combo-box-popup .list-cell:disabled,因为它已经设置好了。
    • 是的,当然在这种情况下只使用:disable 就可以了。但是,在某些用例中,您可能出于其他原因想要禁用“可选”项之一,在这种情况下,不同的伪类可能会很有用。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-03-04
    • 1970-01-01
    相关资源
    最近更新 更多