Kidades
Kidades

Reputation: 670

How to center text inside a ListView?

Is there any way to align text of elements to center inside a ListView in javafx?

I haven't found a method that does this.

Upvotes: 6

Views: 4987

Answers (3)

Alex
Alex

Reputation: 1

Необходимо задать новую фабрику для формирования ячейки ListView. Для этого мы передаем новый объект Callback и в метод setCellFactory, а у ячейки ListCell переопределяем метод call, где задаем правило формирования поступающих данных. Но на самом деле через стили css все делается проще, ответ с css выше был раскрыт.

    listViewOnExit.setCellFactory(new Callback<ListView<String>, ListCell<String>>() {
        @Override
        public ListCell<String> call(ListView<String> stringListView) {
            ListCell<String> cell = new ListCell<String>(){
                @Override
                protected void updateItem(String item, boolean empty) {
                    super.updateItem(item, empty);
                    setText(item);
                }
            };
            cell.setAlignment(Pos.CENTER);
            return cell;
        }
    });

Upvotes: -1

A.Abdelhak
A.Abdelhak

Reputation: 140

try to load the font :

Font.loadFont(getClass().getResourceAsStream("/direct/font_nam .ttf"), 20);

Upvotes: 0

fabian
fabian

Reputation: 82461

You could use a css stylesheet to set the style alignment of ListCells of a ListView. E.g. if the id lv is assigned to the ListView:

#lv .list-cell {
    -fx-alignment: center;
}

Upvotes: 7

Related Questions