How can I wrap text in a ListView?
The following code does not work. I want the text to wrap and prevent horizontal scrollbars.
public class TestApp3 extends Application {
public static void main(String[] args) {
try {
launch(args);
} catch (Exception e) {
e.printStackTrace();
}
}
public void start(final Stage stage) throws Exception {
ListView<String> listView = new ListView<String>();
listView.setItems(FXCollections.observableArrayList("This is very very very very very very very very very very very very very very very long text."));
listView.setCellFactory(new Callback<ListView<String>, ListCell<String>>() {
@Override
public ListCell<String> call(ListView<String> stringListView) {
ListCell<String> cell = new ListCell<String>() {
protected void updateItem(String item, boolean empty) {
super.updateItem(item, empty);
if (!empty) {
// Both of these methods don't work.
setText(item);
setWrapText(true);
Label label = new Label(item);
label.setWrapText(true);
setGraphic(label);
}
}
};
return cell;
}
});
Scene scene = new Scene(listView);
stage.setScene(scene);
stage.show();
}
}