Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/377.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Enter键的Java可编辑JCombobox Keylistener事件_Java_Swing_Jcombobox_Keylistener_Enter - Fatal编程技术网

Enter键的Java可编辑JCombobox Keylistener事件

Enter键的Java可编辑JCombobox Keylistener事件,java,swing,jcombobox,keylistener,enter,Java,Swing,Jcombobox,Keylistener,Enter,我有可编辑的JCombobox,并为combobox编辑器组件添加了keylistener。 当用户按“回车键”时,如果可编辑组合框上没有文本,我需要使用JOptinoPane显示消息框。我已经在keyrelease事件中完成了必要的代码,它按预期显示消息 问题是,当我们得到消息框时,若用户在JOptionPane的“OK”按钮上按enter键,combobox编辑器keyevent会再次触发。因此,当用户按消息框上的Enter键时,JoptionPane会连续显示 你知道怎么解决这个问题吗 请

我有可编辑的JCombobox,并为combobox编辑器组件添加了keylistener。 当用户按“回车键”时,如果可编辑组合框上没有文本,我需要使用JOptinoPane显示消息框。我已经在keyrelease事件中完成了必要的代码,它按预期显示消息

问题是,当我们得到消息框时,若用户在JOptionPane的“OK”按钮上按enter键,combobox编辑器keyevent会再次触发。因此,当用户按消息框上的Enter键时,JoptionPane会连续显示

你知道怎么解决这个问题吗

请注意,我不能为此使用操作侦听器

Note that I can't use Action listener for this.
这没有任何意义,那么使用ItemListener

Any idea how to solve this?
  • 决不要将r用于
    Swing JComponents
    ,而是使用(
    注意,我不能为此
    使用操作侦听器)

  • 注意:默认情况下,
    API
    中的
    JComboBox
    实现了
    ENTER键
    ,必须通过按下
    ENTER键来覆盖此操作


一个选项是用您自己的KeySelectionManager接口替换。您希望替换JComboBox.KeySelectionManager,因为它负责获取输入的字符并返回应选择的行号(作为int)

请检查此代码是否对您有帮助

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);

大多数人觉得这很困难,因为这是一个强制转换。

我们需要在组合框用于服务编辑的组件上添加一个键侦听器

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

希望此代码有帮助。

请通过
ev.getkeycode()
检查事件ascii代码,并检查它是数字还是字符。如果它既不是数字也不是字符,则什么也不做。
如果这是您想要的,则执行此过程。

如果您正在使用Netbeans,则右键单击组合框并选择“自定义代码”。 添加以下代码行

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

请编辑你的问题,包括一个显示你当前方法的问题。特别是所有的casting.writing
((JTextComponent)cmb.getEditor().getEditorComponent()).getText().isEmpty()
,在事件监听器中会增加一些可读性,无论如何,好的解决方案,对我帮助很大!