Stopping JPopupMenu stealing the focus

前端 未结 3 1573
感动是毒
感动是毒 2021-02-05 21:18

I have a JTextField for which I\'m hoping to suggest results to match the user\'s input. I\'m displaying these suggestions in a JList contained within

3条回答
  •  时光说笑
    2021-02-05 21:43

    It looks straight forward to me. Add the following

    field.requestFocus();

    after

     menu.add(list);
     menu.show(field, 0, field.getHeight());
    

    Of course, you will have to code for when to hide the popup etc based on what is going on with the JTextField.

    i.e;

     menu.show(field, field.getX(), field.getY()+field.getHeight());
     menu.setVisible(true);
     field.requestFocus();
    

提交回复
热议问题