wood burning stoves 2.0*
The moose likes Swing / AWT / SWT and the fly likes JComboBox popup question, please help? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "JComboBox popup question, please help?" Watch "JComboBox popup question, please help?" New topic
Author

JComboBox popup question, please help?

Renee Zhang
Ranch Hand

Joined: Sep 10, 2001
Posts: 72
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
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
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://aspose.com/file-tools
 
subject: JComboBox popup question, please help?
 
Similar Threads
JTables editor/renderer ?
JCombobox
key event on JComboBox
JComboBox select onself the item
JComboBox problem - pls help