From 5ebdf64d300354c8ce4795ce60a85b8c2596f5ea Mon Sep 17 00:00:00 2001 From: Karl Tauber Date: Thu, 30 Jul 2020 13:30:50 +0200 Subject: [PATCH] ComboBox: fixed width of popup, which was too small if popup is wider than combo box and vertical scroll bar is visible (issue #137) --- CHANGELOG.md | 2 ++ .../formdev/flatlaf/ui/FlatComboBoxUI.java | 24 +++++++++++++++---- 2 files changed, 21 insertions(+), 5 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 61590d3d..17f148e4 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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. diff --git a/flatlaf-core/src/main/java/com/formdev/flatlaf/ui/FlatComboBoxUI.java b/flatlaf-core/src/main/java/com/formdev/flatlaf/ui/FlatComboBoxUI.java index cb42c5b6..471cc905 100644 --- a/flatlaf-core/src/main/java/com/formdev/flatlaf/ui/FlatComboBoxUI.java +++ b/flatlaf-core/src/main/java/com/formdev/flatlaf/ui/FlatComboBoxUI.java @@ -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;