0

The combo box below has an array of numbers, being: 10, 90 80. etc etc. I want to have it when someone presses enter in the textbox with any value it displays that value in the combobox.

However, I do not want it to physically add it to the list of items in the combo box, only to temporary display it until a value is chosen in the combo box.

Text Box:

    textField = new JTextField();
    textField.setBounds(150,40,80,20);
    getContentPane().add(textField);
    textField.addKeyListener(new keyListener());

Enter listener:

public class keyListener extends KeyAdapter
{
    public void keyPressed(KeyEvent e) 
    {
       if (e.getKeyCode()==KeyEvent.VK_ENTER)
       {
            comboBox.addItem(TEMP_ITEM_HERE);

       }
    }

}

Combo Box:

    comboBox = new JComboBox(cdata);
    comboBox.setBounds(80,300,100,20);
   getContentPane().add(comboBox);
Bradie
  • 85
  • 3
  • 1
    For better help sooner, post a [MCVE] or [Short, Self Contained, Correct Example](http://www.sscce.org/). – Andrew Thompson May 07 '18 at 09:09
  • 1
    .. 2) Java GUIs have to work on different OS', screen size, screen resolution etc. using different PLAFs in different locales. As such, they are not conducive to pixel perfect layout. Instead use layout managers, or [combinations of them](http://stackoverflow.com/a/5630271/418556) along with layout padding and borders for [white space](http://stackoverflow.com/a/17874718/418556). 3) For Swing, we typically use [key bindings](https://docs.oracle.com/javase/tutorial/uiswing/misc/keybinding.html) rather than the lower level `KeyListener`. – Andrew Thompson May 07 '18 at 09:11
  • 1
    Maybe use an editable combo box. Then the user can type the number in the editor of the combo box, instead of having a separate text field. – camickr May 07 '18 at 14:21
  • BTW - it seems you are going to a lot of trouble here, to (re)implement functionality that is mostly in an existing combo box. Give the combo focus and type some letters or numbers, and it will jump to the entry that starts with those characters. What value does a text field for showing the matching combo items offer, over and above that inbuilt functionality? – Andrew Thompson May 08 '18 at 07:31

0 Answers0