From b2831789798a2f2ea586efae22171259da9e051f Mon Sep 17 00:00:00 2001 From: Karl Tauber Date: Fri, 10 Sep 2021 16:37:07 +0200 Subject: [PATCH] Spinner: fixed painting of border corners on left side (issue #382; regression since FlatLaf 1.4) ComboBox (editable): fixed wrong border of internal text field under special circumstances --- CHANGELOG.md | 4 +++ .../formdev/flatlaf/ui/FlatComboBoxUI.java | 33 +++++++------------ .../com/formdev/flatlaf/ui/FlatSpinnerUI.java | 21 +++++++----- .../flatlaf/testing/FlatComponentsTest.java | 4 +++ 4 files changed, 32 insertions(+), 30 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 5c7e1ae7..46723dfe 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -20,6 +20,10 @@ FlatLaf Change Log - OptionPane: Fixed rendering of longer HTML text if it is passed as `StringBuilder`, `StringBuffer`, or any other object that returns HTML text in method `toString()`. (similar to issue #12) +- ComboBox (editable): Fixed wrong border of internal text field under special + circumstances. +- Spinner: Fixed painting of border corners on left side. (issue #382; + regression since FlatLaf 1.4) - TableHeader: Do not show resize cursor for last column if resizing last column is not possible because auto resize mode of table is not off. (issue #332) - TextField, FormattedTextField, PasswordField and ComboBox: Fixed alignment of 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 89f1a424..865e77e7 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 @@ -44,7 +44,6 @@ import java.beans.PropertyChangeListener; import javax.swing.AbstractAction; import javax.swing.BorderFactory; import javax.swing.CellRendererPane; -import javax.swing.ComboBoxEditor; import javax.swing.DefaultListCellRenderer; import javax.swing.InputMap; import javax.swing.JButton; @@ -338,33 +337,25 @@ public class FlatComboBoxUI } @Override - protected ComboBoxEditor createEditor() { - ComboBoxEditor comboBoxEditor = super.createEditor(); + protected void configureEditor() { + super.configureEditor(); - Component editor = comboBoxEditor.getEditorComponent(); if( editor instanceof JTextField ) { JTextField textField = (JTextField) editor; textField.setColumns( editorColumns ); - // assign a non-null and non-javax.swing.plaf.UIResource border to the text field, - // otherwise it is replaced with default text field border when switching LaF - // because javax.swing.plaf.basic.BasicComboBoxEditor.BorderlessTextField.setBorder() - // uses "border instanceof javax.swing.plaf.basic.BasicComboBoxEditor.UIResource" - // instead of "border instanceof javax.swing.plaf.UIResource" - textField.setBorder( BorderFactory.createEmptyBorder() ); + // remove default text field border from editor + Border border = textField.getBorder(); + if( border == null || border instanceof UIResource ) { + // assign a non-null and non-javax.swing.plaf.UIResource border to the text field, + // otherwise it is replaced with default text field border when switching LaF + // because javax.swing.plaf.basic.BasicComboBoxEditor.BorderlessTextField.setBorder() + // uses "border instanceof javax.swing.plaf.basic.BasicComboBoxEditor.UIResource" + // instead of "border instanceof javax.swing.plaf.UIResource" + textField.setBorder( BorderFactory.createEmptyBorder() ); + } } - return comboBoxEditor; - } - - @Override - protected void configureEditor() { - super.configureEditor(); - - // remove default text field border from editor - if( editor instanceof JTextField && ((JTextField)editor).getBorder() instanceof FlatTextBorder ) - ((JTextField)editor).setBorder( BorderFactory.createEmptyBorder() ); - // explicitly make non-opaque if( editor instanceof JComponent ) ((JComponent)editor).setOpaque( false ); 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 c8b4c8bb..fdfdf657 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 @@ -175,15 +175,7 @@ public class FlatSpinnerUI @Override protected JComponent createEditor() { JComponent editor = super.createEditor(); - - // explicitly make non-opaque - editor.setOpaque( false ); - JTextField textField = getEditorTextField( editor ); - if( textField != null ) - textField.setOpaque( false ); - - updateEditorPadding(); - updateEditorColors(); + configureEditor( editor ); return editor; } @@ -191,8 +183,19 @@ public class FlatSpinnerUI protected void replaceEditor( JComponent oldEditor, JComponent newEditor ) { super.replaceEditor( oldEditor, newEditor ); + configureEditor( newEditor ); + removeEditorFocusListener( oldEditor ); addEditorFocusListener( newEditor ); + } + + /** @since 1.6 */ + protected void configureEditor( JComponent editor ) { + // explicitly make non-opaque + editor.setOpaque( false ); + JTextField textField = getEditorTextField( editor ); + if( textField != null ) + textField.setOpaque( false ); updateEditorPadding(); updateEditorColors(); diff --git a/flatlaf-testing/src/main/java/com/formdev/flatlaf/testing/FlatComponentsTest.java b/flatlaf-testing/src/main/java/com/formdev/flatlaf/testing/FlatComponentsTest.java index b74c3aa8..d63e1fd4 100644 --- a/flatlaf-testing/src/main/java/com/formdev/flatlaf/testing/FlatComponentsTest.java +++ b/flatlaf-testing/src/main/java/com/formdev/flatlaf/testing/FlatComponentsTest.java @@ -1621,6 +1621,10 @@ public class FlatComponentsTest // customRenderer.setBorder( new LineBorder( Color.red ) ); // comboBox1.setRenderer( customRenderer ); // comboBox3.setRenderer( customRenderer ); + + // for testing issue #382 +// spinner1.setModel( new SpinnerNumberModel( 0, null, 100, 1 ) ); +// comboBox1.setEditor( new BasicComboBoxEditor() ); } // JFormDesigner - Variables declaration - DO NOT MODIFY //GEN-BEGIN:variables