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.
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.
You could use a css stylesheet to set the style alignment of ListCell
s of a ListView
. E.g. if the id lv
is assigned to the ListView
:
#lv .list-cell {
-fx-alignment: center;
}
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 try to load the font :
Font.loadFont(getClass().getResourceAsStream("/direct/font_nam .ttf"), 20);
Необходимо задать новую фабрику для формирования ячейки 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;
}
});
© 2022 - 2024 — McMap. All rights reserved.