ComboBox: fixed width of popup, which was too small if popup is wider than combo box and vertical scroll bar is visible (issue #137)

This commit is contained in:
Karl Tauber
2020-07-30 13:30:50 +02:00
parent 2640ab2e8b
commit 5ebdf64d30
2 changed files with 21 additions and 5 deletions

View File

@@ -15,6 +15,8 @@ FlatLaf Change Log
- ToolTip: Do not show empty tooltip component if tooltip text is an empty
string. (issue #134)
- Button: Support specifying button border width.
- ComboBox: Fixed width of popup, which was too small if popup is wider than
combo box and vertical scroll bar is visible. (issue #137)
- ComboBox: Changed maximum row count of popup list to 15 (was 20). Set UI value
`ComboBox.maximumRowCount` to any integer to use a different value.

View File

@@ -47,6 +47,7 @@ import javax.swing.JComboBox;
import javax.swing.JComponent;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JScrollBar;
import javax.swing.JTextField;
import javax.swing.KeyStroke;
import javax.swing.ListCellRenderer;
@@ -544,13 +545,26 @@ public class FlatComboBoxUI
@Override
protected Rectangle computePopupBounds( int px, int py, int pw, int ph ) {
// get maximum display size of all items
Dimension displaySize = getDisplaySize();
// get maximum display width of all items
int displayWidth = getDisplaySize().width;
// add border insets
for( Border border : new Border[] { scroller.getViewportBorder(), scroller.getBorder() } ) {
if( border != null ) {
Insets borderInsets = border.getBorderInsets( null );
displayWidth += borderInsets.left + borderInsets.right;
}
}
// add width of vertical scroll bar
JScrollBar verticalScrollBar = scroller.getVerticalScrollBar();
if( verticalScrollBar != null )
displayWidth += verticalScrollBar.getPreferredSize().width;
// make popup wider if necessary
if( displaySize.width > pw ) {
int diff = displaySize.width - pw;
pw = displaySize.width;
if( displayWidth > pw ) {
int diff = displayWidth - pw;
pw = displayWidth;
if( !comboBox.getComponentOrientation().isLeftToRight() )
px -= diff;