繁体   English   中英

失去焦点时可编辑的JComboBox触发ActionListener

[英]Editable JComboBox firing ActionListener when losing focus

我正在编写扩展JComboBox的类( UIPromptComboBox )。 组合框是可编辑的,对于类的一个应用程序,它是通过控制ActionListener来实现的。

当前,在编辑组合框时会触发ActionListener ,这很好。 但是,当我取消选择组合框时也会触发此ActionListener ,并且我无法区分两个事件,也不想在取消选择组合框时触发它。

实施班

private void addUIField() {
        // Initialise and place combobox
        this.myGuiTextField = new UIPromptComboBox();
        myGuiTextField.setSize(COMBO_WIDTH, defaultHeight);
        GuiUtils.positionControl(myPanel, myGuiTextField, myTop, PROMPT_X_LOC);

        //Add action listener
        myGuiTextField.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent evt) {
                if (evt.getActionCommand().equals("comboBoxEdited")) {
                    newUIcreated((UIPromptComboBox) evt.getSource());
                }
            }

            private void newUIcreated(UIPromptComboBox alteredGuiTextField) {
                try {
                    UIPrompt uip = alteredGuiTextField.getUIPrompt(((PowerPointTextItem) myPPTRef).getValue());
                    if (!simInfo.isInPrompts(uip)) {
                        simInfo.addUIPrompt(uip);
                        alteredGuiTextField.addNewUIPrompt(uip);
                    }
                } catch (MissingPowerpointItem ex) {
                    Exceptions.printStackTrace(ex);
                }
            }
        });
    }

扩展JComboBox的类

public class UIPromptComboBox extends JComboBox {

    public UIPromptComboBox(UIPrompt[] items) {
        super(items);
        this.setEditable(true);
    }

    public UIPromptComboBox() {
        this.setEditable(true);
        this.setEnabled(false);
    }

    /**
     * returns either the selected UI prompt or a new prompt using the example
     * text
     *
     * @param exampleText only used if new prompt is created
     * @return UI prompt selected
     */
    public UIPrompt getUIPrompt(String exampleText) {
        UIPrompt uIPrompt = null;
        Object returnedItem = this.getSelectedItem();
        if (returnedItem instanceof UIPrompt) {
            uIPrompt = (UIPrompt) returnedItem;
        } else if (returnedItem instanceof String) {
            uIPrompt = new UIPrompt((String) returnedItem, exampleText);
        }
        return uIPrompt;
    }

    public void addNewUIPrompt(UIPrompt newPrompt) {
        ActionListener[] actionListerners = this.getActionListeners();

        this.removeActionListener(this);
        this.addItem(newPrompt);
        this.setSelectedItem(newPrompt);

        for (ActionListener al : actionListerners) {
            this.addActionListener(al);
        }

    }

    /**
     * Used for displaying a report value sentence
     * i.e. a string that is not associated with UI Prompts
     * @param newText report value sentence
     */
    public void setText(String newText) {
        this.removeAllItems();
        this.addItem(newText);
        this.setSelectedItem(newText);
    }

    /**
     * For when the UI prompts can be added on construction
     *
     * @param currentUIs list of UI promts
     */
    public void addItems(UIPrompt[] currentUIs) {
        this.removeAllItems();
        DefaultComboBoxModel boxModel = new DefaultComboBoxModel(currentUIs);
        this.setModel(boxModel);
    }

}

由于失去焦点而导致的多次触发导致创建多个对象并将其添加到列表中。 我想我可能没有正确实现ActionListener 谢谢您的帮助

如您所说,您仅希望事件在用户按下Enter键时触发。 更好的方法是使用键侦听器而不是动作侦听器。

myGuiTextField.addKeyListener(new KeyAdapter() {
    @Override
    public void keyPressed(KeyEvent e) {
        if (e.getKeyCode() == KeyEvent.VK_ENTER) {
            newUIcreated((UIPromptComboBox) evt.getSource());
        }
    }

    private void newUIcreated(UIPromptComboBox alteredGuiTextField) {
           try {
                UIPrompt uip = alteredGuiTextField.getUIPrompt(((PowerPointTextItem) myPPTRef).getValue());
            if (!simInfo.isInPrompts(uip)) {
                simInfo.addUIPrompt(uip);
                alteredGuiTextField.addNewUIPrompt(uip);
            }
        } catch (MissingPowerpointItem ex) {
            Exceptions.printStackTrace(ex);
        }
    }
});

现在,这应该只触发用户newUIcreated事件,一旦用户按下Enter键,就别无其他选择。 以此替换您的动作监听器

我现在终于找到了问题。

UIPrompt的显示包括添加了有时包含换行符的字符串。

单击另一个字段的操作触发了UIPrompt的呈现,但是当其中包含换行符时,它将再次触发ActionListener 这就是为什么comboBoxEdited重复动作的原因。

暂无
暂无

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

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