A
Arun
I have the code below in a KeyListener which operates upon a JComboBox
(sorry if the formatting is dire).
I have the problem that if the user is typing into the JComboBox too
quickly, some sort of error occurs and the autofill updates too
quickly.
EG if i type in "classfileset", by the time i have written "cl", i get
"classfilesetXXX" with XXX being the letters i type after "cl".
I think the problem is that, when the autofill finds a word that mite
match what the user has typed so far, it fills the field with that
word, then sets the rest of the word to be highlighted.
EG if i type "cl", "assfileset" would be highlighted
However if the user is typing too quickly, the highlight doesnt happen
and so the rest of the user input is entered at the end of the word.
Does anyone know how i get past user input being too quick?
I tried to solve it a couple weeks ago but to no avail.
Code -----------------------------------------
public void keyReleased(KeyEvent arg0)
{
// Gets textbox from comboBox
JTextComponent editor = (JTextComponent)
comboBox.getEditor().getEditorComponent();
// Gets text currently entered into combo box
String currentText = (String) comboBox.getEditor().getItem();
int currentTextSize = currentText.length();
for(int x = 0; x < comboBox.getItemCount(); x++)
{
// Get text user has so far entered
String selectedItem = (String) comboBox.getItemAt(x);
// If the text so far entered matches the start of any text
in the combo box
if (selectedItem.startsWith(currentText))
{
if (Character.isLetterOrDigit(arg0.getKeyChar()))
{
// Set the box to the matched item
comboBox.getEditor().setItem(selectedItem);
// Highlight the following the text
editor.setSelectionStart(currentTextSize);
editor.setSelectionEnd(selectedItem.length());
return;
}
}
}
}
(sorry if the formatting is dire).
I have the problem that if the user is typing into the JComboBox too
quickly, some sort of error occurs and the autofill updates too
quickly.
EG if i type in "classfileset", by the time i have written "cl", i get
"classfilesetXXX" with XXX being the letters i type after "cl".
I think the problem is that, when the autofill finds a word that mite
match what the user has typed so far, it fills the field with that
word, then sets the rest of the word to be highlighted.
EG if i type "cl", "assfileset" would be highlighted
However if the user is typing too quickly, the highlight doesnt happen
and so the rest of the user input is entered at the end of the word.
Does anyone know how i get past user input being too quick?
I tried to solve it a couple weeks ago but to no avail.
Code -----------------------------------------
public void keyReleased(KeyEvent arg0)
{
// Gets textbox from comboBox
JTextComponent editor = (JTextComponent)
comboBox.getEditor().getEditorComponent();
// Gets text currently entered into combo box
String currentText = (String) comboBox.getEditor().getItem();
int currentTextSize = currentText.length();
for(int x = 0; x < comboBox.getItemCount(); x++)
{
// Get text user has so far entered
String selectedItem = (String) comboBox.getItemAt(x);
// If the text so far entered matches the start of any text
in the combo box
if (selectedItem.startsWith(currentText))
{
if (Character.isLetterOrDigit(arg0.getKeyChar()))
{
// Set the box to the matched item
comboBox.getEditor().setItem(selectedItem);
// Highlight the following the text
editor.setSelectionStart(currentTextSize);
editor.setSelectionEnd(selectedItem.length());
return;
}
}
}
}