ComboBox: fixed search in item list for text with spaces (issue #691)

This commit is contained in:
Karl Tauber
2023-08-01 15:19:15 +02:00
parent b47ed94f40
commit ce527329a6
4 changed files with 52 additions and 3 deletions

View File

@@ -35,6 +35,7 @@ FlatLaf Change Log
- Native Windows libraries: Fixed crash when running in Java 8 and newer Java
version is installed in `PATH` environment variable and using class
`SystemInfo` before AWT initialization. (issue #673)
- ComboBox: Fixed search in item list for text with spaces. (issue #691)
- FormattedTextField: On Linux, fixed `IllegalArgumentException: Invalid
location` if `JFormattedTextField.setDocument()` is invoked in a focus gained
listener on that formatted text field. (issue #698)

View File

@@ -50,7 +50,8 @@ class FlatInputMaps
}
modifyInputMap( defaults, "ComboBox.ancestorInputMap",
"SPACE", "spacePopup",
// Space key still shows popup, but from FlatComboBoxUI.FlatKeySelectionManager
// "SPACE", "spacePopup",
"UP", mac( "selectPrevious2", "selectPrevious" ),
"DOWN", mac( "selectNext2", "selectNext" ),

View File

@@ -24,6 +24,7 @@ import java.awt.Component;
import java.awt.ComponentOrientation;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.FontMetrics;
import java.awt.Graphics;
import java.awt.Graphics2D;
@@ -48,10 +49,12 @@ import java.util.concurrent.atomic.AtomicBoolean;
import javax.swing.AbstractAction;
import javax.swing.BorderFactory;
import javax.swing.CellRendererPane;
import javax.swing.ComboBoxModel;
import javax.swing.DefaultListCellRenderer;
import javax.swing.InputMap;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JComboBox.KeySelectionManager;
import javax.swing.JComponent;
import javax.swing.JList;
import javax.swing.JPanel;
@@ -182,6 +185,9 @@ public class FlatComboBoxUI
private void installUIImpl( JComponent c ) {
super.installUI( c );
// install key selection manager that shows popup when Space key is pressed
comboBox.setKeySelectionManager( new FlatKeySelectionManager( comboBox.getKeySelectionManager() ) );
installStyle();
}
@@ -1232,4 +1238,46 @@ public class FlatComboBoxUI
}
}
}
//---- class FlatKeySelectionManager --------------------------------------
/**
* Key selection manager that delegates to the default manager.
* Shows the popup if Space key is pressed and "typed characters" buffer is empty.
* If items contain spaces (e.g. "a b") it is still possible to select them
* by pressing keys a, Space and b.
*/
private class FlatKeySelectionManager
implements JComboBox.KeySelectionManager, UIResource
{
private final KeySelectionManager delegate;
private final long timeFactor;
private long lastTime;
FlatKeySelectionManager( JComboBox.KeySelectionManager delegate ) {
this.delegate = delegate;
Long value = (Long) UIManager.get( "ComboBox.timeFactor" );
timeFactor = (value != null) ? value : 1000;
}
@SuppressWarnings( "rawtypes" )
@Override
public int selectionForKey( char aKey, ComboBoxModel aModel ) {
long time = EventQueue.getMostRecentEventTime();
long oldLastTime = lastTime;
lastTime = time;
// SPACE key shows popup if not yet visible
if( aKey == ' ' &&
time - oldLastTime >= timeFactor &&
!comboBox.isPopupVisible() )
{
comboBox.setPopupVisible( true );
return -1;
}
return delegate.selectionForKey( aKey, aModel );
}
}
}

View File

@@ -23,7 +23,7 @@ CheckBox.focusInputMap [lazy] 2 javax.swing.plaf.InputMapUIResource [
#---- ComboBox ----
ComboBox.ancestorInputMap [lazy] 15 javax.swing.plaf.InputMapUIResource [UI]
ComboBox.ancestorInputMap [lazy] 14 javax.swing.plaf.InputMapUIResource [UI]
alt DOWN togglePopup
alt KP_DOWN togglePopup
alt KP_UP togglePopup
@@ -37,7 +37,6 @@ ComboBox.ancestorInputMap [lazy] 15 javax.swing.plaf.InputMapUIResource
KP_UP selectPrevious2
PAGE_DOWN pageDownPassThrough
PAGE_UP pageUpPassThrough
SPACE spacePopup
UP selectPrevious2