如何在NetBeans向导面板中关闭键侦听器? [英] How to turn off a key listener in NetBeans wizard panels?

查看:161
本文介绍了如何在NetBeans向导面板中关闭键侦听器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我为NetBeans IDE开发了一个简单的插件。我在向导面板的 TopComponenet 上的默认键事件有一点问题:

I developed a simple plugin for NetBeans IDE. I have a little problem with default key event on TopComponenet of Wizard panel:

我有一个包含3个步骤的向导。在第二步中,我有一个 JTextField ,其中用户放置了一些值,然后在此文本字段下面出现 JList 。一切都没问题,直到用户从列表中选择一个值,然后按键 ENTER 然后我的面板转到下一步3.我附上一个键监听器列出如下内容:

I have a wizard with 3 steps. In second step I have a JTextField where user put some values and after that appear a JList below this text field. Everything is okay until user choose a some value from list and than press key ENTER then my panel goes to next Step 3. I attach a key listener to list something like:

list = new JList(new PackagesListModel());
list.addKeyListener(new KeyAdapter() {
    @Override
    public void keyReleased(java.awt.event.KeyEvent evt) {
        int keyCode = evt.getKeyCode();
        if(keyCode == KeyEvent.VK_ENTER){
            JList list = (JList)evt.getSource();
            Object selectedPackage = list.getSelectedValue();
            typePackageField.setText((String)selectedPackage);
        }
    }
});

但是这个监听器可能是在 TopComponenet 关于向导。如何使用ENTER键阻止用户移动到下一步?

But this listener probably is invoking after default listener of TopComponenet on wizard. How can I prevent moving user to next step using ENTER key?

我不想要这个动作(当用户按下ENTER然后进入下一步)。

I don't want this action (when user press ENTER then they go to the next step).

更新:

转发给Kraal回答:

Forwarding to Kraal answer:

问题是我不知道我在哪里可以寻找一个JButton Next (向一个听众说不出话来)。这听起来很奇怪,但我是怎么写的。我使用Netbeans Plaform WizzardDescriptor生成一个Wizzard(有3个步骤)。 WizzardDescriptor来自包:

Problem is that i dont know where i can lookking for a JButton Next (to shuting down a listener). It sound strange but how i wrote. Im using a Netbeans Plaform WizzardDescriptor to generate a Wizzard (with 3 steps) . WizzardDescriptor is from package:

  org.openide.WizardDescriptor; // Dialogs API

我向他展示了3个面板实例: WizardDescriptor.Panel 来自同一个包:

i puted to him a 3 instances of panels: WizardDescriptor.Panel from same package:

  org.openide.WizardDescriptor // Dialogs API

看起来像:

    panels = new ArrayList<>();
    panels.add(new LayoutWizardPanel1(selectedLayout));
    panels.add(new LayoutWizardPanel2(selectedLayout));
    panels.add(new LayoutWizardPanel3(selectedLayout));
    WizardDescriptor wiz = new WizardDescriptor(new WizardDescriptor.ArrayIterator<>(panels));

之后会产生类似的结果:

After this will generated something like:

在我的程序中可以访问WizardDescriptor

in my program i have access to WizardDescriptor

 http://bits.netbeans.org/dev/javadoc/org-openide-dialogs/org/openide/WizardDescriptor.html


推荐答案


一切正常,直到用户从列表中选择一个值,然后按下ENTER键然后我的面板转到下一步3.我附上一个键
监听器来列出像[...]

如果你想在你的列表中 Enter 键优先于默认下一步 - > 按钮然后你必须使用密钥绑定在重点关注时将动作附加到您的列表中:

If you want Enter key in your list has precedence over the default Next -> button then you have to use Key binding to attach an action to your list when it's focused:

KeyStroke enterKeyStroke = KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 0);
Action updateTextfieldAction = new AbstractAction() {
    @Override
    public void actionPerformed(ActionEvent e) {
        JList list = (JList)evt.getSource();
        Object selectedPackage = list.getSelectedValue();
        typePackageField.setText((String)selectedPackage );
    }
};

list = new JList(new PackagesListModel());
list.getInputMap().put(enterKeyStroke, "enter");
list.getActionMap().put("enter", updateTextfieldAction);

请注意 getInputMap() getInputMap(JComponent.WHEN_FOCUSED)。这意味着如果您的列表具有焦点并且按下 Enter 键,则将执行附加到此键击的操作。

Note that getInputMap() is a shortcut for getInputMap(JComponent.WHEN_FOCUSED). This means that if your list has focus and Enter key is pressed, then the action attached to this key stroke will be performed.

由此如果此按钮是默认按钮或者使用 WHEN_IN_FOCUSED_WINDOW 使用键绑定附加了一个动作:

By this way your action will always have precedence over the next button's action, either if this button is the default button or it has attached an action using key bindings using WHEN_IN_FOCUSED_WINDOW like this:

JButton button = new JButton(nextStepAction);
button.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(enterKeyStroke, "enter");
button.getActionMap().put("enter", nextStepAction);

其中 nextStepAction 将是要采取的行动向导的下一步。

Where nextStepAction would be the action to go to wizard's next step.

另请参阅 Java中的键绑定与主要侦听器

请结合示例下面。请注意,如果您关注另一个组件但列出了默认操作。我已将按钮设置为框架的根窗格默认按钮,并且我使用 WHEN_IN_FOCUSED_WINDOW 附加了一个操作,以证明 WHEN_FOCUSED 动作优先于那些。

Please cosider the example below. Note if you focus another component but list the the default action is performed. I've set the button as frame's root pane default button and I've attached an action using WHEN_IN_FOCUSED_WINDOW just to prove that WHEN_FOCUSED action has precedence over those.

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;
import javax.swing.AbstractAction;
import javax.swing.Action;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JList;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextField;
import javax.swing.KeyStroke;
import javax.swing.SwingUtilities;

public class Demo {

    private JList list;
    private JTextField textField;

    private void createAndShowGUI() {

        KeyStroke enterKeyStroke = KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 0);

        Action nextStepAction = new AbstractAction("Next") {
            @Override
            public void actionPerformed(ActionEvent e) {
                JOptionPane.showMessageDialog(null, "Going to step 3!", "Message", JOptionPane.INFORMATION_MESSAGE);
            }
        };

        Action updateTextfieldAction = new AbstractAction() {
            @Override
            public void actionPerformed(ActionEvent e) {
                textField.setText((String)list.getSelectedValue());
            }
        };

        list = new JList(new String[]{"Item 1", "Item 2", "Item 3"});
        list.setPrototypeCellValue("This is a list's prototype cell value.");
        list.getInputMap().put(enterKeyStroke, "enter");
        list.getActionMap().put("enter", updateTextfieldAction);

        textField = new JTextField(15);

        JPanel listPanel = new JPanel();
        listPanel.add(new JScrollPane(list));
        listPanel.add(textField);

        JButton button = new JButton(nextStepAction);
        button.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(enterKeyStroke, "enter");
        button.getActionMap().put("enter", nextStepAction);

        JPanel buttonsPanel = new JPanel(new FlowLayout(FlowLayout.TRAILING));
        buttonsPanel.add(button);

        JFrame frame = new JFrame("Demo");
        frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        frame.add(listPanel);
        frame.add(buttonsPanel, BorderLayout.PAGE_END);
        frame.getRootPane().setDefaultButton(button);
        frame.pack();
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                new Demo().createAndShowGUI();
            }
        });
    }
}






其他评论



请注意,如果你想避免所有这些问题,你可以附上 ListSelectionListener 到您的列表并更新选择更改时的文本字段,无需按 Enter 关键。实际上,如果您可以访问下一步操作,则可以根据列表选择启用/禁用它。通过执行此操作,您将确保如果列表中未选择任何项目,则向导无法继续。恕我直言,这将是一种更优雅的方式来处理这种情况。有关详细信息,请参阅选择列表中的项目


Other comments

Please note that if you want avoid all this matter, you could attach a ListSelectionListener to your list and update the text field on selection change with no need to press Enter key at all. In fact if you have access to the next step action you could enable/disable it based on list selection. By doing this you'll be sure that wizard can't continue if no item is selected in your list. IMHO that would be a more elegant way to handle this situation. See Selecting Items in a List for further details.

这篇关于如何在NetBeans向导面板中关闭键侦听器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆