File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Swing / AWT / SWT and the fly likes JComboBox problem - pls help Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "JComboBox problem - pls help " Watch "JComboBox problem - pls help " New topic

JComboBox problem - pls help

Sajee Joseph
Ranch Hand

Joined: Jan 17, 2001
Posts: 200
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.

[This message has been edited by Sajee Joseph (edited December 21, 2001).]
Dorj Galaa
Ranch Hand

Joined: May 29, 2001
Posts: 113
try this

public class MagicComboBox extends JComboBox {

public MagicComboBox() {
ComboAgent agent = new ComboAgent(this);
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();
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++)>
for (int k = 0; k < m_comboBox.getItemCount(); k++)
String item = m_comboBox.getItemAt(k).toString().toLowerCase();
if (item.startsWith(str))

Senior software engineer
Paul Stevens
Ranch Hand

Joined: May 17, 2001
Posts: 2823
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.
I agree. Here's the link:
subject: JComboBox problem - pls help
It's not a secret anymore!