Hi,

My JTable has a cell editor implemented as an instance of the DefaultCellEditor(JComboBox) class.

I am using a subclass of JComboBox to give the auto complete functionality to the user. The code of this subclass I have got from the net (I don't remember the source now), and is given below:

Java Code:
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import javax.swing.*;
import javax.swing.text.*;

public class AutoCompleteComboBox extends JComboBox implements JComboBox.KeySelectionManager
{
	private String searchFor;
	private long lap;
	public class CBDocument extends PlainDocument
	{
        @Override
		public void insertString(int offset, String str, AttributeSet a) throws BadLocationException
		{
			if (str==null) return;
			super.insertString(offset, str, a);
			if(!isPopupVisible() && str.length() != 0) [COLOR="Red"]fireActionEvent();[/COLOR]
		}
	}
	public AutoCompleteComboBox(Object[] items)
	{
		super(items);
		lap = new java.util.Date().getTime();
		[COLOR="DeepSkyBlue"]setKeySelectionManager(this);[/COLOR]
		JTextField tf;
		if(getEditor() != null)
		{
			tf = (JTextField)getEditor().getEditorComponent();
			if(tf != null)
			{
				tf.setDocument(new CBDocument());
				addActionListener(new ActionListener()
				{
					public void actionPerformed(ActionEvent evt)
					{
						JTextField tf = (JTextField)getEditor().getEditorComponent();
						String text = tf.getText();
						ComboBoxModel aModel = getModel();
						String current;
						for(int i = 0; i < aModel.getSize(); i++)
						{
							current = aModel.getElementAt(i).toString();
							if(current.toLowerCase().startsWith(text.toLowerCase()))
							{
								tf.setText(current);
								tf.setSelectionStart(text.length());
								tf.setSelectionEnd(current.length());
								break;
							}
						}
					}
				});
			}
		}
	}
	[COLOR="DeepSkyBlue"]public int selectionForKey(char aKey, ComboBoxModel aModel)
	{
		long now = new java.util.Date().getTime();
		if (searchFor!=null && aKey==KeyEvent.VK_BACK_SPACE &&	searchFor.length()>0)
		{
			searchFor = searchFor.substring(0, searchFor.length() -1);
		}
		else
		{
			//	System.out.println(lap);
			// Kam nie hier vorbei.
			if(lap + 1000 < now)
				searchFor = "" + aKey;
			else
				searchFor = searchFor + aKey;
		}
		lap = now;
		String current;
		for(int i = 0; i < aModel.getSize(); i++)
		{
			current = aModel.getElementAt(i).toString().toLowerCase();
			if (current.toLowerCase().startsWith(searchFor.toLowerCase())) return i;
		}
		return -1;
	}
	public void fireActionEvent()
	{
		super.fireActionEvent();
	}[/COLOR]

// I don't think that the code highlighted in Sky Blue has relevance to the problem raised in this post,
// but I have included the code for the completeness.

}
The problem I am facing is that as soon as the user starts typing in the combo box, it comes out of the editing mode, and thus effectively preventing entering any value.

On scrutinizing the code of the subclass, I found that it invokes the fireActionEvent method whenever a string is inserted in its text editor (the relevant line has been highlighted in red in the code). This as I understand causes the editor to comes out of the editing mode. The function invocation however is important and can't be left out as it calls the action listeners and thereby the auto complete code is executed.

I tried to alter the code in various ways so that on inserting a string the fireActionEvent is not invoked, but the auto complete code is called nonetheless. One way I tried to implement is by adding a bound property and running the auto complete code in the the propertychange listener in place of running it from the ActionListener. However, none of the methods has worked, and I am very frustrated now.

Surely, the auto complete functionality I am trying to provide is not very advanced one, and there must be some way to accomplish this. I will be grateful I you could point me towards the solution.

Thanks in advance,
Dinesh