ComboBox: support custom borders in combobox editors (issue #102)

This commit is contained in:
Karl Tauber
2020-05-23 18:26:59 +02:00
parent d2d4f73834
commit 4af8d2f1c5
2 changed files with 22 additions and 8 deletions

View File

@@ -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)

View File

@@ -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 );