Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: HOW TO NAVIGATE THROUGH COMBOBOX USING UP AND DOWN ARROW KEYS

  1. #1
    Junior Member
    Join Date
    Oct 2020
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default HOW TO NAVIGATE THROUGH COMBOBOX USING UP AND DOWN ARROW KEYS

    txtfCustomer = new JTextField();
    txtfCustomer.setFont(new Font("Times New Roman", Font.BOLD, 14));
    txtfCustomer.addKeyListener(new KeyAdapter() {
    @SuppressWarnings("unchecked")
    @Override
    public void keyReleased(KeyEvent e) {
    try {
    cboName.removeAllItems();
    String val = txtfCustomer.getText().trim();
    if (!val.equals("")) {
    String query = "select distinct CUSTOMER from TBL_DRUG_123 where CUSTOMER like '%" + val
    + "%' ";
    Statement pst = conn.createStatement();
    ResultSet rs = pst.executeQuery(query);

    while (rs.next()) {
    cboName.addItem(rs.getString("CUSTOMER"));

    }
    }

    } catch (Exception e1) {
    e1.printStackTrace();

    }
    }

    @SuppressWarnings("deprecation")
    @Override
    public void keyPressed(KeyEvent e) {

    if (e.getKeyCode() == KeyEvent.VK_ENTER) {

    String str = (String) cboName.getSelectedItem();
    txtfCustomer.setText(str);
    cboName.setVisible(false);
    cboName.setPopupVisible(false);
    cboName.hidePopup();
    cboName.hide();
    txtfRate.requestFocus();
    txtfRate.setBackground(Color.CYAN);
    if (e.getKeyCode() == KeyEvent.VK_DOWN) {

    //WHAT IS THE CODE HERE ??????
    if (e.getKeyCode() == KeyEvent.VK_UP) {

    //WHAT IS THE CODE HERE ??????
    }
    }

    }
    }

    public void keyTyped(KeyEvent e) {
    contentPane.add(cboName);
    cboName.setVisible(true);
    cboName.setPopupVisible(true);
    cboName.showPopup();
    }
    });
    txtfCustomer.setColumns(10);
    txtfCustomer.setBorder(null);
    txtfCustomer.setBounds(96, 89, 109, 26);
    contentPane.add(txtfCustomer);

    cboName = new JComboBox();
    cboName.setFont(new Font("Times New Roman", Font.BOLD, 14));
    cboName.setSelectedItem("");
    AutoCompleteDecorator.decorate(cboName);
    cboName.setBounds(220, 278, 159, 22);
    //contentPane.add(cboName);

  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,166
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: HOW TO NAVIGATE THROUGH COMBOBOX USING UP AND DOWN ARROW KEYS

    Note: There is missing code from what is posted. The posted code can not be compiled and executed for testing.
    Please post a small, complete program that can be compiled and executed for testing.

    Please edit your post and wrap your code with code tags:

    [code]
    **YOUR CODE GOES HERE**
    [/code]

    to get highlighting and preserve formatting.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. How can I rotate an image using arrow keys?
    By DPaul1994 in forum What's Wrong With My Code?
    Replies: 0
    Last Post: May 15th, 2014, 12:03 PM
  2. Please help with Arrow Keys
    By rtucker1023 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: April 4th, 2013, 04:58 PM
  3. Moving an image around the screen using the arrow keys.
    By nemo in forum What's Wrong With My Code?
    Replies: 4
    Last Post: March 19th, 2013, 12:08 AM
  4. [SOLVED] Making an image move with arrow keys
    By Clex19 in forum What's Wrong With My Code?
    Replies: 4
    Last Post: July 6th, 2012, 09:45 AM
  5. [SOLVED] Arrow keys to navigate a maze in a console program
    By B25Mitch in forum What's Wrong With My Code?
    Replies: 9
    Last Post: January 31st, 2012, 04:58 PM