• Post Reply Bookmark Topic Watch Topic
  • New Topic

How to drop the list of a JcomboBox?  RSS feed

 
xiao li
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Swing Gurus,
I have set a label for a comboBox, and the label has it mnemonic key setup. When I use Alt+mnemonic key of the label, I want to see the focus move to the comboBox and the list box should drop. (right now the list can drop only when the mouse is pointing the comboBox)
Thansk a lot.
 
lavanya subramanian
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi xiao li

add a key listener to the container that contains this combobox and label and in the event code add the popup method of combobox
the code somewhat look like this
your_container.addKeyListener(new KeyAdapter()
{
public void keyPressed(KeyEvent ke)
{

if(ke.getKeyCode() == KeyEvent.VK_your-mnemonic-character goes here)
{
combobox.showPopup();
}

}
});
alternatively you can close the combobox with hidePopup() method.

i think this would help you,
lavanya
 
xiao li
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Lavanya,
I tried your suggestions, add KeyListener to the comboBox's container, and add one more variable m_ProcessKey. and this is my added code:
/**
* Handle the key pressed event
*/
public void keyPressed(KeyEvent e)
{
if(!m_ProcessKey && e.getKeyCode()==18)//ALT=18
{
m_ProcessKey = true;
}
}
/**
* Handle the key released event
*/
public void keyReleased(KeyEvent e)
{
if(m_ProcessKey && e.getKeyCode()==18)//ALT=18
{
m_ProcessKey = false;
}
}

/**
* Handle the key typed event
*/
public void keyTyped(KeyEvent e)
{
if(m_ProcessKey)
{
if(e.getKeyChar()=='m') // my mnemonic key is M
{
m_Combo.showPopup();
}
else
{
m_Combo.hidePopup();
}
}
}
It works! Thanks a lot.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!