簡體   English   中英

ScalaFX / JavaFX:如何更改ComboBox的溢出樣式?

[英]ScalaFX/JavaFX: How can I change the overrun style of a ComboBox?

我需要設置所選項目的溢出樣式 據我所知,要設置溢出樣式,我需要訪問buttonCell (類型為ObjectProperty[javafx.scene.control.ListCell[T]] )。

因此我寫了

val fileComboBox = new ComboBox[java.io.File](Seq())
println(fileComboBox.buttonCell)

為了查看buttonCell成員具有哪個值。

結果: [SFX]ObjectProperty [bean: ComboBox@319f91f9[styleClass=combo-box-base combo-box], name: buttonCell, value: null] ,這意味着沒有可以設置其溢出樣式的按鈕單元格( value: null )。

如何更改組合框的溢出樣式?

您可以使用外部CSS文件執行此操作:

.combo-box > .list-cell {
  -fx-text-overrun: leading-ellipsis ;
}

有效值為[ center-ellipsis | center-word-ellipsis | clip | ellipsis | leading-ellipsis | leading-word-ellipsis | word-ellipsis ] [ center-ellipsis | center-word-ellipsis | clip | ellipsis | leading-ellipsis | leading-word-ellipsis | word-ellipsis ] [ center-ellipsis | center-word-ellipsis | clip | ellipsis | leading-ellipsis | leading-word-ellipsis | word-ellipsis ]其中ellipsis為默認值。

您也可以通過直接設置按鈕單元格來執行此操作。 在JavaFX中(我將讓您將其翻譯為Scala):

ListCell<File> buttonCell = new ListCell<File>() {
    @Override
    protected void updateItem(File item, boolean empty) {
        super.updateItem(item, empty);
        setText(empty ? null : item.getName());
    }
};
buttonCell.setTextOverrun(OverrunStyle.LEADING_ELLIPSIS);
fileComboBox.setButtonCell(buttonCell);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM