Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

JComboBox problem - pls help

 
Sajee Joseph
Ranch Hand
Posts: 200
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I have a JComboBox. Let it contain some items like "a","b", "c", etc. Now i want to do an activity when the person changes the selection in the combo. Let me explain in detail. Ket us assume that the JCombo is currently displaying "a". Now when the user selects "b" and moves out of focus of the JCombo, i need to an activity. What i maen is that i dont want to do the actvity just when the selected item is changed (coz when he scrolls down the list, the itemchanged will be fired). Also i dont want to do the activity when he deselects "a", then selects "b", and then comes back to "a"(all withought going out of focus of the combo).
Earlier i thought that for this i would store the intial selected item("a") in a temporary variable, and on focus lost event of the jcombo, only if the selected item is other than the intial selection i will do the activity. But again By JComboBox is the only thing in the frame, so the focus lost event is not triggered,coz there is no other elemnet to put focus. But whne i clcik the mouse on any empty place of the frame, the focus lost for the combo should be triggered. its not happeneing so. What do i do? how do i solve it?
Waiting for ur replies.
Regards
Saj

[This message has been edited by Sajee Joseph (edited December 21, 2001).]
 
Dorj Galaa
Ranch Hand
Posts: 113
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
try this

public class MagicComboBox extends JComboBox {

public MagicComboBox() {
super();
ComboAgent agent = new ComboAgent(this);
setEditable(true);
}
}
class ComboAgent extends KeyAdapter
{
protected JComboBox m_comboBox;
protected JTextField m_editor;
public ComboAgent(MagicComboBox comboBox)
{
m_comboBox = comboBox;
m_editor = (JTextField)comboBox.getEditor().getEditorComponent();
m_editor.addKeyListener(this);
}
public void keyReleased(KeyEvent e)
{
char ch = e.getKeyChar();
boolean isFound=false;
if (ch == KeyEvent.CHAR_UNDEFINED | | Character.isISOControl(ch)) return;
int pos = m_editor.getCaretPosition();
String str = m_editor.getText().toLowerCase();
String srcstring=str;
if (str.length() == 0) return;
for(int i=0;i<srcstring.length();i++)>
{
str=srcstring.substring(0,i+1);
for (int k = 0; k < m_comboBox.getItemCount(); k++)
{
String item = m_comboBox.getItemAt(k).toString().toLowerCase();
if (item.startsWith(str))
{
m_editor.setText(m_comboBox.getItemAt(k).toString());
isFound=true;
m_comboBox.setSelectedIndex(k);
m_editor.setCaretPosition(item.length());
m_editor.moveCaretPosition(pos);
m_comboBox.showPopup();
break;
}
}
}
if(!isFound)
{
m_editor.setText(m_comboBox.getItemAt(0).toString());
m_comboBox.setSelectedIndex(0);
m_comboBox.showPopup();
}
}
}
 
Paul Stevens
Ranch Hand
Posts: 2823
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You could also add a GO button beside it. It would be similar to many web based implementations. It also leaves the choice to do it up to the user.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic