JavaFX ListView without horizontal scroll

When we use the standar JavaFX ListView we can have the problem that the text is wider than the available place.

As a consequence, the List View will show Horizontal scrollbars.

I thought about an easy solution of it and I made it. The solution can be found here:

https://github.com/Ciruman/Sandbox/tree/master/JavaFXEllipsisListCell

The interesting part is:


/**
* @author Diego Cirujano
*/
public class EllipsisListCell
            extends ListCell<String> {

    private final Label label = new Label();
    private final StackPane pane;

    public EllipsisListCell() {
        pane = new StackPane();
        pane.setStyle("-fx-border-color:red;");
        pane.setMinWidth(0);
        pane.setPrefWidth(1);
        pane.getChildren().add(label);
    }

    @Override
    protected void updateItem(String item
                            , boolean empty) {
        if (empty || item == null) {
            setGraphic(null);
            setText("");
        } else {
            label.setText(item);
            setGraphic(pane);
        }
    }
}

Result:

I should spend some time to check if it would make sense to add this feature JavaFX Jira. Time to have a look to the original implementation.

Advertisements

One thought on “JavaFX ListView without horizontal scroll

  1. Thank you very much for the example. I found that using this directly, I could no longer click on the items in the list. If you add “super.updateItem(item, empty);” in the overridden updateItem(…) method, it allows me to select items.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s