D. Jung
D. Jung

Reputation: 145

JavaFX ListView SetCellFactory add margin between imageView and Text

I'll use a ListView as navigation menu. So I found out how to add an ImageView next to the ListCell title using ListView.setCellFactory. But now I want to have more space between ImageView and title. How can I achieve this? This is my code:

        lvNavigation.setCellFactory(listView -> new ListCell<String>(){
        private ImageView imageView = new ImageView();

        @Override
        public void updateItem(String name, boolean empty) {
            super.updateItem(name, empty);

            if (empty) {
                setText(null);
                setGraphic(null);
            } else {
                Image image = getImageFromStatus(name);
                imageView.setImage(image);
                setText(name);
                setGraphic(imageView);
            }
        }
    });

getImageFromStatus(name) returns an Image object. Thats how it actually looks like:

enter image description here

Upvotes: 0

Views: 474

Answers (1)

Itai
Itai

Reputation: 6911

Use the graphicTextGapProperty :

setGraphicTextGap(10.0); // Or any other appropriate value

Upvotes: 2

Related Questions