The Artima Developer Community
Sponsored Link

Java Answers Forum
Problem with dropdown

1 reply on 1 page. Most recent reply: Dec 17, 2004 3:52 AM by mausam

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 1 reply on 1 page
vipin.singh

Posts: 3
Nickname: vips
Registered: Sep, 2004

Problem with dropdown Posted: Sep 26, 2004 4:31 PM
Reply to this message Reply
Advertisement
Hi All,
I am using a JCombobox control.
I have a panel and a dropdown in this panel. If in a drop down I have some values say one entry is Vipin and other is Ikon. My requirement is if some one moves focus to the drop down and presses V and then i (Vi) it should select Vipin. Currently what is hapenning if some is typing V and then i it is selecting entry starting with I (Ikon). I need a solution for this problem urgently. If any one has clue on how to go ahead with solving this problem, please reply.
Thanks for the help in advance.
Regards,
Vipin.


mausam

Posts: 243
Nickname: mausam
Registered: Sep, 2003

Re: Problem with dropdown Posted: Dec 17, 2004 3:52 AM
Reply to this message Reply
public class AminurKeyKeyListener implements KeyListener
{
	JList listOfData;
	int startCounter = 0;
	boolean currentSearchFound =false;
	public AminurKeyKeyListener (JList listOfData)
	{
		this.listOfData = listOfData;
		startCounter=0;
	}
	public void keyTyped(KeyEvent ke)
	{
	}
	
	public void keyPressed(KeyEvent ke)
	{	
		char charPressed = ke.getKeyChar();
		startCounter = listOfData.getSelectedIndex();
		//The string object is created to change the charPressed to LowerCase.
		char [] tempCharArray = {charPressed};
		String charString =  new String(tempCharArray);
		charString = charString.toLowerCase();
		charPressed = charString.charAt(0);
		ListModel listModel = listOfData.getModel();
		currentSearchFound=false;
		ListCellRenderer cellRenderer = listOfData.getCellRenderer();
		
		for(int i=startCounter+1;i<listModel.getSize();i++)
		{
			String test = ((javax.swing.JLabel)(cellRenderer.getListCellRendererComponent(listOfData,listModel.getElementAt(i),0,false,false))).getText();		
			if (test!=null && test.length()!=0)
			{
			
				if (charPressed==test.trim().toLowerCase().charAt(0))
				{
					listOfData.setSelectedIndex(i);
					listOfData.ensureIndexIsVisible(i);
					currentSearchFound=true;
					break;
				}
				currentSearchFound=false;
			}	
		}
        if (!currentSearchFound  && startCounter !=0  )
		{
		   	for (int k=0;k<startCounter ;k++ )
		   	{
			    String test = ((javax.swing.JLabel)(cellRenderer.getListCellRendererComponent(listOfData,listModel.getElementAt(k),0,false,false))).getText();		
				if (test!=null && test.length()!=0)
				{
					if (charPressed==test.trim().toLowerCase().charAt(0))
					{
						listOfData.setSelectedIndex(k);
						listOfData.ensureIndexIsVisible(k);
						break;
					}
					currentSearchFound=false;
				}
				
		   	}
		} 
	}
	
	public void keyReleased(KeyEvent ke)
	{	
	}
 
}


add a key listener to ur list and do something like the code above.

In my code if u press V, vipin gets selected... now u will have to store v and i also to differenciate between vipin and veneet. I have done it for JList, you do it for JComboBox

Flat View: This topic has 1 reply on 1 page
Topic: an interesting swing focus question Previous Topic   Next Topic Topic: java combobox

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use