I am trying to build a text editor. I'm using a ListView for storing comments. I want the string to be wrapped automatically without me having to scroll through it horizontally so that long string doesn't have to be scrolled so much. Please suggest a way to wrap the string in the ListView.
EDIT 1: My ListView is initialized from FXML EDIT 2: I could finally get the initialize working.
All you have to do is to override the default cell, with your own custom cell whose width is the width of the List
and also allows text to wrap. The implementation is based on the fact that Cell
s are Labled
list.setCellFactory(param -> new ListCell<DataModel>(){
@Override
protected void updateItem(String item, boolean empty) {
super.updateItem(item, empty);
if (empty || item==null) {
setGraphic(null);
setText(null);
// other stuff to do...
}else{
// set the width's
setMinWidth(param.getWidth());
setMaxWidth(param.getWidth());
setPrefWidth(param.getWidth());
// allow wrapping
setWrapText(true);
setText(item.toString());
}
}
});