Search code examples
javascalajavafxellipsisscalafx

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


I need to set the overrun style of the selected item. To set the overrun style, as far as I understand, I need to access the buttonCell (which is of type ObjectProperty[javafx.scene.control.ListCell[T]]).

Hence I wrote

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

in order to see which value the buttonCell member has.

Result: [SFX]ObjectProperty [bean: ComboBox@319f91f9[styleClass=combo-box-base combo-box], name: buttonCell, value: null], which means there is no button cell the overrun style of which I could set (value: null).

How can I change the overrun style of the combo box?


Solution

  • You can do this with an external CSS file:

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

    The valid values are [ center-ellipsis | center-word-ellipsis | clip | ellipsis | leading-ellipsis | leading-word-ellipsis | word-ellipsis ] with ellipsis being the default.

    You can also do this by setting the button cell directly. In JavaFX (I'll leave you to translate it to 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);