void (@NotNull JList<?> list) { int size = list.getModel().getSize(); if (size == 0) { list.clearSelection(); return; } int selectedIndex = list.getSelectedIndex(); if (selectedIndex < 0 || selectedIndex >= size) { // fit index to [0, size-1] range selectedIndex = 0; } selectItem(list, selectedIndex); }