@@ -93,10 +93,15 @@ private void adjustPopupWidth() {
9393 if (scrollPane == null ) {
9494 return ;
9595 }
96- Window window = SwingUtilities .getWindowAncestor (popup );
97- if (window == null ) {
98- return ;
99- }
96+
97+ // Heavyweight popup → its own top-level Window (distinct from the combo's window).
98+ // Lightweight popup → no dedicated Window; getWindowAncestor returns the *main app frame*,
99+ // which we must NOT resize. Whether a popup is heavy or light is decided per-show by Swing
100+ // based on whether it fits inside the parent window (Windows tends to use lightweight when
101+ // it fits; macOS uses heavyweight more often).
102+ Window comboWindow = SwingUtilities .getWindowAncestor (comboBox );
103+ Window popupWindow = SwingUtilities .getWindowAncestor (popup );
104+ boolean heavyweight = popupWindow != null && popupWindow != comboWindow ;
100105
101106 // Clear sizes set by BasicComboPopup.getPopupLocation() so the scroll pane
102107 // reports its natural content-based width.
@@ -107,18 +112,20 @@ private void adjustPopupWidth() {
107112 int naturalWidth = scrollPane .getPreferredSize ().width ;
108113 int targetWidth = Math .min (Math .max (naturalWidth , comboBox .getWidth ()), ComboBoxPopup .MAX_WIDTH );
109114
110- // Use the window's actual height — correctly determined by BasicComboPopup based on row count.
111- // Setting scroller maxSize to windowHeight lets BoxLayout fill all available vertical space .
112- int windowHeight = window .getHeight ();
113- Dimension scrollSize = new Dimension (targetWidth , windowHeight );
115+ // For the height, BasicComboPopup has already sized the popup's container correctly based
116+ // on row count. Reuse that height when available; otherwise fall back to popup pref height .
117+ int targetHeight = heavyweight ? popupWindow .getHeight () : popup . getPreferredSize (). height ;
118+ Dimension scrollSize = new Dimension (targetWidth , targetHeight );
114119 scrollPane .setPreferredSize (scrollSize );
115120 scrollPane .setMaximumSize (scrollSize );
116- popup .setPreferredSize (new Dimension (targetWidth , windowHeight ));
121+ popup .setPreferredSize (new Dimension (targetWidth , targetHeight ));
117122
118- // Widen the popup window; the height was already set correctly by BasicComboPopup.
119- Point loc = window .getLocation ();
120- window .setSize (targetWidth , windowHeight );
121- window .setLocation (loc );
123+ if (heavyweight ) {
124+ // Widen the dedicated popup window; preserve its location.
125+ Point loc = popupWindow .getLocation ();
126+ popupWindow .setSize (targetWidth , targetHeight );
127+ popupWindow .setLocation (loc );
128+ }
122129
123130 popup .revalidate ();
124131 popup .repaint ();
0 commit comments