Source Code : Displaying the Menu in a JComboBox Component Using a Keystroke

Java Is Open Source Programming Language You Can Download From Java and Java Libraries From http://www.oracle.com. Click Here to download
We provide this code related to title for you to solve your developing problem easily. Libraries which is import in this program you can download from http://www.oracle.com. Click Here or search from google with Libraries Name you get jar file related it

Displaying the Menu in a JComboBox Component Using a Keystroke

  

import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;

import javax.swing.JComboBox;

public class Main {
  public static void main(String[] argv) throws Exception {
    String[] items = { "A", "A", "B", "B", "C", "C" };
    JComboBox cb = new JComboBox(items);

    // Create and register the key listener
    cb.addKeyListener(new MyKeyListener());

  }
}

class MyKeyListener extends KeyAdapter {
  public void keyPressed(KeyEvent evt) {
    JComboBox cb = (JComboBox) evt.getSource();

    // Get pressed character
    char ch = evt.getKeyChar();

    // If not a printable character, return
    if (ch != KeyEvent.CHAR_UNDEFINED) {
      cb.showPopup();
    }
  }
}

   
    
  

Thank with us