ComboBox: improved location of selected item in popup if list is large and scrollable

This commit is contained in:
Karl Tauber
2023-07-30 14:01:24 +02:00
parent 9db3dfff26
commit b3fb63c9f5
2 changed files with 25 additions and 0 deletions

View File

@@ -8,6 +8,8 @@ FlatLaf Change Log
- Extras: Class `FlatSVGIcon` now uses [JSVG](https://github.com/weisJ/jsvg) - Extras: Class `FlatSVGIcon` now uses [JSVG](https://github.com/weisJ/jsvg)
library (instead of svgSalamander) for rendering. JSVG provides improved SVG library (instead of svgSalamander) for rendering. JSVG provides improved SVG
rendering and uses less memory compared to svgSalamander. (PR #684) rendering and uses less memory compared to svgSalamander. (PR #684)
- ComboBox: Improved location of selected item in popup if list is large and
scrollable.
- Added system property `flatlaf.useNativeLibrary` to allow disabling loading of - Added system property `flatlaf.useNativeLibrary` to allow disabling loading of
FlatLaf native library. (issue #674) FlatLaf native library. (issue #674)

View File

@@ -986,6 +986,29 @@ public class FlatComboBoxUI
} }
} }
// improve location of selected item in popup if list is large and scrollable
if( list.getHeight() == 0 ) {
// If popup is shown for the first time (or after a laf switch) and is scrollable,
// then BasicComboPopup scrolls the selected item to the top of the visible area.
// But for usability it would be better to have selected item somewhere
// in the middle of the visible area so that the user can see items above
// the selected item, which are usually more "important".
int selectedIndex = list.getSelectedIndex();
if( selectedIndex >= 1 ) {
int maximumRowCount = comboBox.getMaximumRowCount();
if( selectedIndex < maximumRowCount ) {
// selected item is in the first visible items --> scroll to top
list.scrollRectToVisible( new Rectangle() );
} else {
// scroll the selected item to the middle of the visible area
int firstVisibleIndex = Math.max( selectedIndex - (maximumRowCount / 2), 0 );
if( firstVisibleIndex > 0 )
list.ensureIndexIsVisible( firstVisibleIndex );
}
}
}
super.show( invoker, x, y ); super.show( invoker, x, y );
} }