Skip to Main Content

Java SE (Java Platform, Standard Edition)

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

How to wrap text in a ListView?

cshApr 24 2013 — edited Apr 26 2013
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();
    }
}
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on May 24 2013
Added on Apr 24 2013
3 comments
1,890 views