To emphasize the last item in a list, you can follow this method:
listView.getItems().get(list.size()-1).setStyle("-fx-font-weight: bold");
However, please note that this approach does not automatically update the list when new items are added in the future.
Here is an example of a list view that can be updated dynamically:
public class Main extends Application {
private ObservableList<Label> list;
@Override
public void start(Stage primaryStage) {
try {
AnchorPane root = new AnchorPane();
Scene scene = new Scene(root,400,400);
scene.getStylesheets().add(getClass().getResource("application.css").toExternalForm());
primaryStage.setScene(scene);
primaryStage.show();
ListView<Label> l = new ListView<>();
root.getChildren().add(l);
list = l.getItems();
list.addListener(new ListChangeListener<Label>() {
@Override
public void onChanged(
javafx.collections.ListChangeListener.Change<? extends Label> c) {
for(Label l : list){
l.setStyle("-fx-font-weight: normal");
}
list.get(list.size()-1).setStyle("-fx-font-weight: bold");
}
});
l.getItems().add(new Label("test"));
l.getItems().add(new Label("test"));
} catch(Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
launch(args);
}
}
I understand that iterating over a list may not be the most elegant solution, but it is efficient in this case. If you prefer, you could create an update method to handle style changes only when necessary, such as after adding a batch of items.
Cheers,
Laurenz