使 ActionHandler 忽略箭头键 [英] Make ActionHandler Ignore Arrow Keys

查看:37
本文介绍了使 ActionHandler 忽略箭头键的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何让 Action Handler 忽略箭头键?目前,当我尝试使用箭头键导航我的组合框时,组合框向下/向上移动一次,更改选择,然后触发操作处理程序将焦点移动到按钮.我希望能够使用箭头键导航组合框,并在我准备好进入下一个组件时按 Enter.

How do I make Action Handler ignore arrow keys? Currently, when I try to use arrow keys to navigate my combo box, the combo box moves down/up once, changes the selection, and then triggers the Action Handler moving the focus to the button. I would like to be able to navigate the combo box with the arrow keys, and hit Enter when I'm ready to move on to the next component.

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class test {

JFrame window = new JFrame("testGUI");
JPanel windowPanel = new JPanel();

public static JLabel labelSize;
public static JComboBox<String> comboSize;

public static JLabel labelButton;
public static JButton buttonButton;

public test () {
    super();

    labelSize = new JLabel("Monster Size:");
    String[] sizeChoices = { "None", "Tiny", "Small", "Medium", "Large", "Huge", "Colossal"};
    comboSize = new JComboBox<String>(sizeChoices);
    comboSize.setToolTipText("The creature's size.");
    comboSize.addKeyListener(new KeyListener() {
        @Override
        public void keyTyped(KeyEvent e) {
            if(e.getKeyCode() == KeyEvent.VK_ENTER){
                comboSize.showPopup();
            }
        }
        @Override
        public void keyReleased(KeyEvent e) {
            comboSize.showPopup();
        }
        @Override
        public void keyPressed(KeyEvent e) {
        }
    });

    labelButton = new JLabel("Button:");
    buttonButton = new JButton();
    buttonButton.addKeyListener(new KeyListener() {
        @Override
        public void keyTyped(KeyEvent e) {
            if(e.getKeyCode() == KeyEvent.VK_ENTER)
                buttonButton.doClick();
        }
        @Override
        public void keyReleased(KeyEvent e) {
        }
        @Override
        public void keyPressed(KeyEvent e) {
            buttonButton.doClick();
        }
    });


    windowPanel.setLayout(new FlowLayout());
    windowPanel.setComponentOrientation(ComponentOrientation.LEFT_TO_RIGHT);
    windowPanel.add(labelSize);
    windowPanel.add(comboSize);
    windowPanel.add(labelButton);
    windowPanel.add(buttonButton);
    windowPanel.setVisible(true);

    window.setSize(500, 500);
    window.setLayout(new FlowLayout());
    window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    window.setVisible(true);
    window.add(windowPanel);

    comboSize.addActionListener(handler);
    buttonButton.addActionListener(handler);
}

ActionHandler handler = new ActionHandler();
public class ActionHandler implements ActionListener {
    public void actionPerformed(ActionEvent eventFocus){
        if (eventFocus.getSource() == comboSize){
            buttonButton.requestFocusInWindow();
        }
        if (eventFocus.getSource() == buttonButton){
            comboSize.requestFocusInWindow();
        }
    }
}

@SuppressWarnings("unused")
public static void main(String[] args) {
    test GUITest = new test();
}
}

推荐答案

组合框向下/向上移动一次,更改选择,然后触发 Action Handler 将焦点移动到按钮上.我希望能够使用箭头键导航组合框,并在我准备好进入下一个组件时按 Enter.

the combo box moves down/up once, changes the selection, and then triggers the Action Handler moving the focus to the button. I would like to be able to navigate the combo box with the arrow keys, and hit Enter when I'm ready to move on to the next component.

您可以使用以下方法在组合框上设置一个属性,以仅在按下 Enter 时生成 ActionEvent:

You can set a property on the combo box to only generate the ActionEvent when Enter is pressed by using the following:

comboBox.putClientProperty("JComboBox.isTableCellEditor", Boolean.TRUE);

完整示例:

/*
    This works on non editable combo boxes
*/
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.plaf.basic.*;
import javax.swing.text.*;

public class ComboBoxAction extends JFrame implements ActionListener
{
    public ComboBoxAction()
    {
        JComboBox<String> comboBox = new JComboBox<String>();
        comboBox.addActionListener( this );

        comboBox.addItem( "Item 1" );
        comboBox.addItem( "Item 2" );
        comboBox.addItem( "Item 3" );
        comboBox.addItem( "Item 4" );

        //  This prevents action events from being fired when the
        //  up/down arrow keys are used on the dropdown menu

        comboBox.putClientProperty("JComboBox.isTableCellEditor", Boolean.TRUE);

        getContentPane().add( comboBox );
        getContentPane().add( new JTextField(), BorderLayout.SOUTH );
    }

    public void actionPerformed(ActionEvent e)
    {
        System.out.println( e.getModifiers() );

        JComboBox comboBox = (JComboBox)e.getSource();
        System.out.println( comboBox.getSelectedItem() );

        //  make sure popup is closed when 'isTableCellEditor' is used

//      comboBox.hidePopup();
    }

    public static void main(String[] args)
    {
        ComboBoxAction frame = new ComboBoxAction();
        frame.setDefaultCloseOperation( EXIT_ON_CLOSE );
        frame.pack();
        frame.setVisible( true );
     }
}

请参阅组合框无操作更多信息.

这篇关于使 ActionHandler 忽略箭头键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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