0

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?

ideaboxer
  • 3,863
  • 8
  • 43
  • 62
  • I wouldn't have thought that you would be able to set this on a combo box. It's not meaningful for a control that requires a line by line selection from the user. You can set the min/max/preferred width though. https://docs.oracle.com/javase/8/javafx/api/javafx/scene/layout/Region.html#setMinWidth-double- – ManoDestra Mar 28 '16 at 15:00
  • The selected value is a path to a file which can get longer than the window width (especially if the window is resized to a small size). Which part of the path is more important: the head or the tail? – ideaboxer Mar 28 '16 at 21:06
  • Is it possible to store just the filename in your combobox display, but store the full path in its value? Provided the filename is unique, of course. – ManoDestra Mar 28 '16 at 21:19
  • Of course it is. But then I still have to display the path somewhere, otherwise the user can not be sure which file is meant. Hence I prefer the easiest solution: Show directory path + basename at once. – ideaboxer Mar 28 '16 at 21:30

1 Answers1

2

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);
James_D
  • 201,275
  • 16
  • 291
  • 322
  • I just tried the CSS setting (which I really like, many thanks for pointing me to that!), and it works :-) – ideaboxer Mar 28 '16 at 21:07