简体   繁体   English

用于 Enter 键的 Java 可编辑 JCombobox Keylistener 事件

[英]Java Editable JCombobox Keylistener event for Enter key

I have editable JCombobox and I added keylistener for combobox editor component.我有可编辑的 JCombobox,并为组合框编辑器组件添加了 keylistener。 When user press 'Enter key' and if there is no text on the editable combobox I need to display message box using JOptinoPane.当用户按下“回车键”并且可编辑组合框上没有文本时,我需要使用 JOptinoPane 显示消息框。 I have done necessary code in keyrelease event and it displays message as expected.我已经在 keyrelease 事件中完成了必要的代码,它按预期显示了消息。

Problem is, when we get message box and if user press enter key on 'OK' button of JOptionPane, combobox editor keyevent fires again.问题是,当我们收到消息框时,如果用户按下 JOptionPane 的“确定”按钮上的 Enter 键,组合框编辑器 keyevent 会再次触发。 Because of this, when user press Enter key on message box, JoptionPane displays continuously.因此,当用户在消息框中按下 Enter 键时,JoptionPane 会持续显示。

Any idea how to solve this?知道如何解决这个问题吗?

Note that I can't use Action listener for this.请注意,我不能为此使用 Action 侦听器。

Please check if this code helps you!!!请检查此代码是否对您有帮助!!!

JFrame frame = new JFrame("Welcome!!");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

JComboBox cmb = new JComboBox();
cmb.setEditable(true);
cmb.getEditor().getEditorComponent().addKeyListener(new KeyAdapter() {

    @Override
    public void keyReleased(KeyEvent event) {
        if (event.getKeyChar() == KeyEvent.VK_ENTER) {
            if (((JTextComponent) ((JComboBox) ((Component) event
                    .getSource()).getParent()).getEditor()
                    .getEditorComponent()).getText().isEmpty())
                System.out.println("please dont make me blank");
        }
    }
});
frame.add(cmb);

frame.setLocationRelativeTo(null);
frame.setSize(300, 50);
frame.setVisible(true);

Most people find it difficult because of this casting.大多数人因为这种铸造而感到困难。

We need to add a key listener on the component that the combo box is using to service the editing.我们需要在组合框用于为编辑提供服务的组件上添加一个关键侦听器。

JTextComponent editor = (JTextComponent) urCombo.getEditor().getEditorComponent();
editor.addKeyListener(new KeyAdapter() {
   public void keyReleased(KeyEvent evt) {
      // your code
   }
});

Hope this code helps.希望这段代码有帮助。

Note that I can't use Action listener for this.

this doesn't make me any sence, then to use ItemListener这对我没有任何意义,然后使用 ItemListener

Any idea how to solve this?
  • never to use KeyListene r for Swing JComponents , use ( Note that I can't use Action listener for this .) KeyBindings instead,永远不要将KeyListene r 用于Swing JComponentsNote that I can't use Action listener for thisNote that I can't use Action listener for this 。) KeyBindings

  • notice ENTER key is implemented for JComboBox in API by default, have to override this action from ENTER key pressed注意ENTER key默认在APIJComboBox实现,必须从ENTER key pressed覆盖此操作

One option would be to replace the KeySelectionManager interface with your own.一种选择是用您自己的接口替换 KeySelectionManager 接口。 You want to replace the JComboBox.KeySelectionManager as it is responsible for taking the inputted char and returns the row number (as an int) which should be selected.您想替换 JComboBox.KeySelectionManager 因为它负责获取输入的字符并返回应选择的行号(作为整数)。

Please check the event ascii code by ev.getkeycode() and check if it is a number or character.请通过ev.getkeycode()检查事件 ascii 代码,并检查它是数字还是字符。 If it is neither a number nor a character do nothing.如果它既不是数字也不是字符,则什么也不做。 If it is what you want then do the process.如果这是您想要的,那么请执行该过程。

If you are using Netbeans then right click on your combobox and select customize code.如果您使用的是 Netbeans,则右键单击您的组合框并选择自定义代码。 add following lines of code添加以下代码行

JTextComponent editor = (JTextComponent) Code.getEditor().getEditorComponent();
editor.addKeyListener(new KeyAdapter() {
   public void keyReleased(KeyEvent evt) {
        if(evt.getKeyCode()==10)
            //do your coding here.
   }
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM