From 8fcce349d554f8840f8278f6bcaa75a779f04b42 Mon Sep 17 00:00:00 2001 From: Karl Tauber Date: Wed, 7 Apr 2021 01:39:29 +0200 Subject: [PATCH] ComboBox and Spinner: fixed too wide arrow button if component is higher than preferred (issue #302) --- CHANGELOG.md | 2 ++ .../formdev/flatlaf/ui/FlatComboBoxUI.java | 19 +++++++++++- .../com/formdev/flatlaf/ui/FlatSpinnerUI.java | 4 +-- .../testing/FlatTextComponentsTest.java | 26 ++++++++++++++++ .../testing/FlatTextComponentsTest.jfd | 30 ++++++++++++++++--- 5 files changed, 74 insertions(+), 7 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index c85b5392..c077b2e8 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -28,6 +28,8 @@ FlatLaf Change Log - IntelliJ Themes: Fixed system colors. - Button and ToggleButton: Do not paint background of disabled (and unselected) toolBar buttons. (issue #292; regression since fixing #112) +- ComboBox and Spinner: Fixed too wide arrow button if component is higher than + preferred. (issue #302) - SplitPane: `JSplitPane.setContinuousLayout(false)` did not work. (issue #301) - TabbedPane: Fixed NPE when creating/modifying in another thread. (issue #299) - Fixed crash when running in Webswing. (issue #290) 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 7efd747e..2b335cf0 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 @@ -243,7 +243,24 @@ public class FlatComboBoxUI public void layoutContainer( Container parent ) { super.layoutContainer( parent ); - if ( editor != null && padding != null ) { + if( arrowButton != null ) { + Insets insets = getInsets(); + int buttonWidth = parent.getPreferredSize().height - insets.top - insets.bottom; + if( buttonWidth != arrowButton.getWidth() ) { + // set width of arrow button to preferred height of combobox + int xOffset = comboBox.getComponentOrientation().isLeftToRight() + ? arrowButton.getWidth() - buttonWidth + : 0; + arrowButton.setBounds( arrowButton.getX() + xOffset, arrowButton.getY(), + buttonWidth, arrowButton.getHeight() ); + + // update editor bounds + if( editor != null ) + editor.setBounds( rectangleForCurrentValue() ); + } + } + + if( editor != null && padding != null ) { // fix editor bounds by subtracting padding editor.setBounds( FlatUIUtils.subtractInsets( editor.getBounds(), padding ) ); } diff --git a/flatlaf-core/src/main/java/com/formdev/flatlaf/ui/FlatSpinnerUI.java b/flatlaf-core/src/main/java/com/formdev/flatlaf/ui/FlatSpinnerUI.java index 45d30547..17c826d2 100644 --- a/flatlaf-core/src/main/java/com/formdev/flatlaf/ui/FlatSpinnerUI.java +++ b/flatlaf-core/src/main/java/com/formdev/flatlaf/ui/FlatSpinnerUI.java @@ -375,8 +375,8 @@ public class FlatSpinnerUI Rectangle editorRect = new Rectangle( r ); Rectangle buttonsRect = new Rectangle( r ); - // make button area square - int buttonsWidth = r.height; + // make button area square (if spinner has preferred height) + int buttonsWidth = parent.getPreferredSize().height - insets.top - insets.bottom; buttonsRect.width = buttonsWidth; if( parent.getComponentOrientation().isLeftToRight() ) { diff --git a/flatlaf-testing/src/main/java/com/formdev/flatlaf/testing/FlatTextComponentsTest.java b/flatlaf-testing/src/main/java/com/formdev/flatlaf/testing/FlatTextComponentsTest.java index 06f722cd..67820c16 100644 --- a/flatlaf-testing/src/main/java/com/formdev/flatlaf/testing/FlatTextComponentsTest.java +++ b/flatlaf-testing/src/main/java/com/formdev/flatlaf/testing/FlatTextComponentsTest.java @@ -74,6 +74,10 @@ public class FlatTextComponentsTest JComboBox comboBox3 = new JComboBox<>(); JLabel spinnerLabel = new JLabel(); JSpinner spinner1 = new JSpinner(); + JSpinner spinner2 = new JSpinner(); + JSpinner spinner3 = new JSpinner(); + JComboBox comboBox2 = new JComboBox(); + JComboBox comboBox4 = new JComboBox(); JPopupMenu popupMenu1 = new JPopupMenu(); JMenuItem cutMenuItem = new JMenuItem(); JMenuItem copyMenuItem = new JMenuItem(); @@ -97,6 +101,10 @@ public class FlatTextComponentsTest "[50,fill]" + "[50,fill]" + "[]" + + "[]para" + + "[]" + + "[]" + + "[]" + "[]")); //---- textFieldLabel ---- @@ -307,6 +315,24 @@ public class FlatTextComponentsTest spinner1.setName("spinner1"); add(spinner1, "cell 1 7,growx"); + //---- spinner2 ---- + spinner2.setName("spinner2"); + add(spinner2, "cell 1 8,growx,height 40"); + + //---- spinner3 ---- + spinner3.setName("spinner3"); + add(spinner3, "cell 1 9,growx,hmax 14"); + + //---- comboBox2 ---- + comboBox2.setEditable(true); + comboBox2.setName("comboBox2"); + add(comboBox2, "cell 1 10,growx,height 40"); + + //---- comboBox4 ---- + comboBox4.setEditable(true); + comboBox4.setName("comboBox4"); + add(comboBox4, "cell 1 11,growx,hmax 14"); + //======== popupMenu1 ======== { popupMenu1.setName("popupMenu1"); diff --git a/flatlaf-testing/src/main/java/com/formdev/flatlaf/testing/FlatTextComponentsTest.jfd b/flatlaf-testing/src/main/java/com/formdev/flatlaf/testing/FlatTextComponentsTest.jfd index abb9aa08..22bb01b8 100644 --- a/flatlaf-testing/src/main/java/com/formdev/flatlaf/testing/FlatTextComponentsTest.jfd +++ b/flatlaf-testing/src/main/java/com/formdev/flatlaf/testing/FlatTextComponentsTest.jfd @@ -1,4 +1,4 @@ -JFDML JFormDesigner: "7.0.0.0.194" Java: "13.0.1" encoding: "UTF-8" +JFDML JFormDesigner: "7.0.3.1.342" Java: "16" encoding: "UTF-8" new FormModel { contentType: "form/swing" @@ -10,7 +10,7 @@ new FormModel { add( new FormContainer( "com.formdev.flatlaf.testing.FlatTestPanel", new FormLayoutManager( class net.miginfocom.swing.MigLayout ) { "$layoutConstraints": "ltr,insets dialog,hidemode 3" "$columnConstraints": "[][][::100][100,fill][fill]" - "$rowConstraints": "[][][][50,fill][50,fill][50,fill][][]" + "$rowConstraints": "[][][][50,fill][50,fill][50,fill][][]para[][][][]" } ) { name: "this" add( new FormComponent( "javax.swing.JLabel" ) { @@ -233,9 +233,31 @@ new FormModel { }, new FormLayoutConstraints( class net.miginfocom.layout.CC ) { "value": "cell 1 7,growx" } ) + add( new FormComponent( "javax.swing.JSpinner" ) { + name: "spinner2" + }, new FormLayoutConstraints( class net.miginfocom.layout.CC ) { + "value": "cell 1 8,growx,height 40" + } ) + add( new FormComponent( "javax.swing.JSpinner" ) { + name: "spinner3" + }, new FormLayoutConstraints( class net.miginfocom.layout.CC ) { + "value": "cell 1 9,growx,hmax 14" + } ) + add( new FormComponent( "javax.swing.JComboBox" ) { + name: "comboBox2" + "editable": true + }, new FormLayoutConstraints( class net.miginfocom.layout.CC ) { + "value": "cell 1 10,growx,height 40" + } ) + add( new FormComponent( "javax.swing.JComboBox" ) { + name: "comboBox4" + "editable": true + }, new FormLayoutConstraints( class net.miginfocom.layout.CC ) { + "value": "cell 1 11,growx,hmax 14" + } ) }, new FormLayoutConstraints( null ) { "location": new java.awt.Point( 0, 0 ) - "size": new java.awt.Dimension( 530, 340 ) + "size": new java.awt.Dimension( 530, 580 ) } ) add( new FormContainer( "javax.swing.JPopupMenu", new FormLayoutManager( class javax.swing.JPopupMenu ) ) { name: "popupMenu1" @@ -252,7 +274,7 @@ new FormModel { "text": "Paste" } ) }, new FormLayoutConstraints( null ) { - "location": new java.awt.Point( 0, 390 ) + "location": new java.awt.Point( 0, 680 ) "size": new java.awt.Dimension( 91, 87 ) } ) }