Hello Friends,

I need a hint to auto complete the combobox.

I have two table SpeciesMaster and BreedMaster (Access Database)

In the java code on the action Listener of the Species combo Box the breed details gets populated in the breed combobox.

But now I want to filter the breed combobox in such a way that if a user presses any character, then all the breed which has that character should get populated in that combo box.

The breed combo box is an editable combo box.

Can anyone help me out in this?

Thanks in advance.

Regards

you can do the filtering on the database. this is actually very easy in sql based databases.

perhaps a key up handler should be defined. every time a key is released, the database should be queried for records that contain the string you have in your input field, or whatever it is, and then the query result should populate your combobox.

querying database records that contain a string on a field is as easy as:
SELECT * FROM table WHERE field LIKE(%str%);

the % character is important because based on it the database knows if you are looking for records that start with str, or if it does not matter where str is within the desired queries. just do some searching for 'sql like' and you will be fine.

you can do the filtering on the database. this is actually very easy in sql based databases.

perhaps a key up handler should be defined. every time a key is released, the database should be queried for records that contain the string you have in your input field, or whatever it is, and then the query result should populate your combobox.

querying database records that contain a string on a field is as easy as:
SELECT * FROM table WHERE field LIKE(%str%);

the % character is important because based on it the database knows if you are looking for records that start with str, or if it does not matter where str is within the desired queries. just do some searching for 'sql like' and you will be fine.

I have added the following qote but it is not working as expected.

cboBreed.getEditor().getEditorComponent().addKeyListener(new java.awt.event.KeyListener(){
	public void keyTyped(java.awt.event.KeyEvent kevt) 
	{
		System.out.println("Inside Key Listener event");
		try
		{
			System.out.println("Inside the try block");
			char ch=kevt.getKeyChar();	
			System.out.println("Character key typed: "+ch);
			con=ConnectionPooling.getConnection();
			Statement stmtBreed=con.createStatement();
			ResultSet rs=null;
			String ss="select BreedName from breedmaster where BreedName like '"+'%'+ch+'%'+"'";
			rs=stmtBreed.executeQuery(ss); 

			if(rs!=null)
			{
				while(rs.next())
				{
					System.out.println("In the while loop");
					cboBreed.setSelectedItem(String.valueOf(ch));
				}
			}
		}
		catch(SQLException excpt)
		{
			excpt.printStackTrace();
		}
	}
	public void keyPressed(KeyEvent e){}
	public void keyReleased(KeyEvent e){}
});

Please let me know what mistake I am doing.

try this:

"select BreedName from breedmaster where BreedName like(%"+ch+"%)";

this could be one problem. hope this works, if it does not, let us know what exactly is happening so that we can help you more.

try this:

"select BreedName from breedmaster where BreedName like(%"+ch+"%)";

this could be one problem. hope this works, if it does not, let us know what exactly is happening so that we can help you more.

I have written the following code

cboBreed.getEditor().getEditorComponent().addKeyListener(new java.awt.event.KeyListener(){
	public void keyTyped(java.awt.event.KeyEvent kevt) 
	{
		try
		{
			char ch=kevt.getKeyChar();	
			con=ConnectionPooling.getConnection();
			Statement stmtBreed=con.createStatement();
			ResultSet rs=null;
			String getspec=cboSpecies.getSelectedItem().toString();
			String ss="select BreedName from breedmaster where SpeciesName='"+getspec+"' and BreedName like '"+ch+'%'+"'";
			rs=stmtBreed.executeQuery(ss); 
			if(rs!=null)
			{
				if (!rs.next()) 
				{ 
					JOptionPane.showMessageDialog(null, "<html><p><font color=\"#FF8C00\" " +
							"size=\"4\" face=\"Book Antiqua\">Select correct breed name"+
							"</font></p></html>" ,"Error:",JOptionPane.ERROR_MESSAGE);
				} 
				else
				{     
					do 
					{     									
						String name=rs.getString(1);
						cboBreed.setSelectedItem(name);
					}while(rs.next());
				}
			}
			con.close();
		}
		catch(SQLException excpt)
		{
			excpt.printStackTrace();
		}
	}
	public void keyPressed(KeyEvent e){}
	public void keyReleased(KeyEvent e){}
});

The query is working fine but I am facing the following issues
1. Whenever I type a character, even that character comes in the editor of the drop down of breed at the end along with the breed name eg. Bulldogb ---- b gets added at the end
2. If there are more than one breed with same starting alphabet the last breed gets selected in the drop down
3. After the user enters any key in the editor of the breed combo box I want the combo box list to be displayed to the user

But I have no ideas as to how to proceed.

Please help me in this.

Thanks,
Pooja

Edited 5 Years Ago by poojavb: spelling mistake

well,
I am not sure I can help you much with these issues you are having. However, I would like to just give you some feedback which might help you rethink things and perhaps find a solution.

your line fifteen says "do line 17 if there is no more results in rs"

AND, you reset the selected item on line 26 every time there is a result in rs. naturally, this causes the last result on you result set to be selected as the item on cboBreed.

hope this helps a bit.

well,
I am not sure I can help you much with these issues you are having. However, I would like to just give you some feedback which might help you rethink things and perhaps find a solution.

your line fifteen says "do line 17 if there is no more results in rs"

AND, you reset the selected item on line 26 every time there is a result in rs. naturally, this causes the last result on you result set to be selected as the item on cboBreed.

hope this helps a bit.

Can anyone help me in how to clear the editor of combo box?
Not the items present in the combo box but just the editor since I have set the setEditable property of JComboBox to true.

Edited 5 Years Ago by poojavb: reason

Can anyone help me in how to clear the editor of combo box?
Not the items present in the combo box but just the editor since I have set the setEditable property of JComboBox to true.

The answer is
combo.getEditor().setItem(null);

This question has already been answered. Start a new discussion instead.