I have a JComboBox which contains more than a thound questions. I understand that a JComboBox = a TextField + a Button + a List. By using SearchComboAgent searchAgent = new SearchComboAgent(myComboBox), I can display the first match data in the textField, But how can I auto Scroll the list and show the item in the list? Please help! public class SearchComboAgent extends KeyAdapter { protected JComboBox m_comboBox; protected JTextField m_editor; public SearchComboAgent(JComboBox comboBox) { m_comboBox = comboBox; m_editor = (JTextField)comboBox.getEditor().getEditorComponent(); m_editor.addKeyListener(this); }
public void keyReleased(KeyEvent e) { char ch = e.getKeyChar(); if (ch == KeyEvent.CHAR_UNDEFINED | | Character.isISOControl(ch) | | ch == KeyEvent.VK_ENTER) return; int pos = m_editor.getCaretPosition(); String str = m_editor.getText(); if (str.length() == 0) return; for (int k = 0; k < m_comboBox.getItemCount(); k++) { String item = m_comboBox.getItemAt(k).toString(); if (item.startsWith(str)) { m_editor.setText(item); m_editor.setCaretPosition(item.length()); m_editor.moveCaretPosition(pos); m_comboBox.setPopupVisible(true); break; } } } } Thanks in advance!
Sajee Joseph
Ranch Hand
Joined: Jan 17, 2001
Posts: 200
posted
0
Hi, Modify the public void keyReleased(KeyEvent e) method as given below. Note that i havejuzt added one statement m_comboBox.setSelectedIndex(k);
public void keyReleased(KeyEvent e) { char ch = e.getKeyChar(); if (ch == KeyEvent.CHAR_UNDEFINED | | Character.isISOControl(ch) | | ch == KeyEvent.VK_ENTER) return; int pos = m_editor.getCaretPosition(); String str = m_editor.getText(); if (str.length() == 0) return; for (int k = 0; k < m_comboBox.getItemCount(); k++) { String item = m_comboBox.getItemAt(k).toString(); if (item.startsWith(str)) { m_editor.setText(item); m_editor.setCaretPosition(item.length()); m_editor.moveCaretPosition(pos); m_comboBox.setSelectedIndex(k); m_comboBox.setPopupVisible(true); break; } } }
I hope this solves the prob. Regards Saj
Renee Zhang
Ranch Hand
Joined: Sep 10, 2001
Posts: 72
posted
0
Thank you so much! Sajee! By moving m_comboBox.setSelectedIndex(k); to the first line of the block, I can see the item in the list. But the problem is that an event is triggered by calling setSelectedIndex(k). I am wondering if there is another solution. Thanks again! Regards Renee
I agree. Here's the link: http://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to
run our stuff on 16 servers instead of 3.