diff --git a/CHANGELOG.md b/CHANGELOG.md index 3987ba2e..0e3816e9 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,7 @@ FlatLaf Change Log - ScrollBar: Made styling more flexible by supporting insets and arc for track and thumb. (issue #103) +- ComboBox: Support custom borders in combobox editors. (issue #102) - Ubuntu Linux: Fixed poorly rendered font. (issue #105) 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 8e987a79..d8adc016 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 @@ -39,6 +39,7 @@ import java.beans.PropertyChangeListener; import java.lang.ref.WeakReference; import javax.swing.AbstractAction; import javax.swing.BorderFactory; +import javax.swing.ComboBoxEditor; import javax.swing.DefaultListCellRenderer; import javax.swing.InputMap; import javax.swing.JButton; @@ -46,6 +47,7 @@ import javax.swing.JComboBox; import javax.swing.JComponent; import javax.swing.JList; import javax.swing.JPanel; +import javax.swing.JTextField; import javax.swing.KeyStroke; import javax.swing.ListCellRenderer; import javax.swing.LookAndFeel; @@ -254,18 +256,29 @@ public class FlatComboBoxUI return new FlatComboPopup( comboBox ); } + @Override + protected ComboBoxEditor createEditor() { + ComboBoxEditor comboBoxEditor = super.createEditor(); + + Component editor = comboBoxEditor.getEditorComponent(); + if( editor instanceof JTextField ) { + JTextField textField = (JTextField) editor; + + // 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(); - // assign a 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" - if( editor instanceof JTextComponent ) - ((JTextComponent)editor).setBorder( BorderFactory.createEmptyBorder() ); - // explicitly make non-opaque if( editor instanceof JComponent ) ((JComponent)editor).setOpaque( false );