简体   繁体   English

在输入字段中按Enter时,关闭自定义对话框

[英]Close custom dialog when Enter is pressed in input field

I've designed (using the GUI designer within Netbeans) a small dialog with two radio buttons and a number spinner. 我已经设计(使用Netbeans中的GUI设计器)了一个带有两个单选按钮和一个数字微调器的小对话框。

  1. If I press Enter while the focus is on one of the radio buttons, the dialog correctly closes, but if the focus is on the spinner, I have to Tab away from it in order to use the Enter key. 如果当焦点位于其中一个单选按钮上时按Enter键,则对话框将正确关闭,但是如果焦点位于微调器上,则必须使用Tab键使其远离以使用Enter键。
    1. How do I instruct the dialog that Enter really means "accept and close"? 如何指示对话框Enter确实意味着“接受并关闭”?
    2. Alternatively, how do I instruct (each) input field to relay an Enter to the "accept and close" handler? 或者,如何指示(每个)输入字段将Enter中继到“接受并关闭”处理程序?
    3. Similarly, how do I instruct the dialog that Esc really means "cancel and close" even when the focus is on the spinner (or other field)? 同样,即使焦点在微调框(或其他字段)上,如何指示对话框Esc真正意味着“取消并关闭”?

how do I instruct (each) input field to relay an Enter to the "accept and close" handler? 如何指示(每个)输入字段将Enter中继到“接受并关闭”处理程序?

The easiest approach is to define a "default button" on the dialog. 最简单的方法是在对话框上定义“默认按钮”。 Then when Enter is pressed the default button will be activated. 然后,当按下Enter键时,默认按钮将被激活。 Check out Enter Key and Button for different ways to do this. 查看“输入键和按钮”以了解执行此操作的不同方法。

how do I instruct the dialog that Esc really means "cancel and close" 我如何指示对话框Esc真正意味着“取消并关闭”

Use Key Bindings to invoke the Action of your Cancel button. 使用Key Bindings来调用“取消”按钮的操作。

First you define an Action to be used by the button: 首先定义一个Action ,以通过按钮来使用:

public class CancelAction extends AbstractAction
{
    public CancelAction()
    {
        super("Cancel");
        putValue( Action.MNEMONIC_KEY, new Integer(KeyEvent.VK_C) );
    }

    @Override
    public void actionPerformed(ActionEvent e)
    {
        Window window = KeyboardFocusManager.getCurrentKeyboardFocusManager().getActiveWindow();

        if (window != null)
        {
            WindowEvent windowClosing = new WindowEvent(window, WindowEvent.WINDOW_CLOSING);
            window.dispatchEvent(windowClosing);
        }
    }
}

Then you add the Action to the button so the user can use the mouse: 然后将“ Action添加到按钮,以便用户可以使用鼠标:

CancelAction cancelAction = new CancelAction();
cancelButton.setAction( cancelAction );
dialog.add(cancelButton);

Now you can use Key Bindings to bind the Escape key to the CancelAction so the user can use the keyboard: 现在,您可以使用“键绑定”将Escape键绑定到CancelAction,以便用户可以使用键盘:

KeyStroke escapeKeyStroke = KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0, false);
getRootPane().getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(escapeKeyStroke, "ESCAPE");
getRootPane().getActionMap().put("ESCAPE", cancelAction);

I suspect the reason I had problems is that a spinner is really a compound control , and the text (well, number) field is an component of that. 我怀疑我遇到问题的原因是微调器实际上是一个复合控件 ,而text(well,number)字段是该控件的组成部分。 So I needed to hook up the events to that subcomponent , rather than to the spinner itself: 因此, 我需要将事件连接到该子组件 ,而不是旋转器本身:

// Make Ok/Cancel work when JSpinner has focus
    getSpinnerField(jSpinnerOffset).addActionListener(new java.awt.event.ActionListener() {
        @Override
        public void actionPerformed(java.awt.event.ActionEvent evt) {
            doOk();
        }
    });

where "getSpinnerField()" is just a shorthand in a private method: 其中“ getSpinnerField()”只是私有方法的简写形式:

private JFormattedTextField getSpinnerField(JSpinner spinner) {
    return ((JSpinner.DefaultEditor) spinner.getEditor()).getTextField();
}

Doing this, the Esc key automagically becomes able to dismiss the dialog. 这样做,Esc键自动变为可以关闭对话框。

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

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