How to center text inside a ListView?
Asked Answered
A

3

6

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.

Aharon answered 20/12, 2015 at 23:11 Comment(0)
M
7

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;
}
Mccarley answered 21/12, 2015 at 0:6 Comment(2)
is it possible to align a specific cell only?Diphenyl
@koulini You need a way write a selector for the ListCell you want to style. The only possibilities for selecting cells based on content or index are the :empty, :filled, :odd and :even pseudoclasses. To change this on a specific item that is not selected/focused, you need to write a custom cellFactory returning cells that update a style class/pseudoclass based on the content.Mccarley
S
0

try to load the font :

Font.loadFont(getClass().getResourceAsStream("/direct/font_nam .ttf"), 20);
Skatole answered 21/12, 2015 at 8:58 Comment(0)
B
-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;
        }
    });
Broil answered 27/4, 2021 at 7:28 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.