Set the width of items list in ChoiceBox JavaFX

社会主义新天地 提交于 2020-01-17 08:21:27

问题


I'd like to see the list of items the same width like it's title:

But actual picture is:

And I haven't found any styles of ChoiceBox's item list in scene builder. Is there any style to add in css file to make items list the same width and position?

Current styles:

ChoiceBox {
    -fx-min-width: 28px;
    -fx-min-height: 42px;

    -fx-font-size: 16px;
    -fx-background-radius: 0;

    -fx-background-color: #ffffff;
    -fx-border-width: 1px;
    -fx-region-background: #000000 /
    -fx-mark-color: #757575;
    -fx-border-color: #C5C5C5;
}

Element declaration:

    @FXML private ChoiceBox channelPicker;
    private ObservableList<String> channelItems = FXCollections.observableArrayList("DAC 1", "DAC 2", "DAC 3", "DAC 4");

    channelPicker.setItems(channelItems);
    channelPicker.getSelectionModel().selectFirst();

Thanks in advance!


回答1:


You can add a context-menu option to your choice-box css as follow:

.choice-box {
    -fx-pref-width: 220;
}

.choice-box .context-menu {
    -fx-pref-width: 220; /* Your width for the context menu background shown */
}


来源:https://stackoverflow.com/questions/43782742/set-the-width-of-items-list-in-choicebox-javafx

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!