java - 使 ActionHandler 忽略箭头键

标签 java swing user-interface

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

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

最佳答案

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:

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

参见Combo Box No Action了解更多信息。

关于java - 使 ActionHandler 忽略箭头键,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43830825/

相关文章:

java - 在 Swing 中,如何在没有特定组件的情况下应用 KeyListener

java - 使用数据库数据填充 jTable

html - 我希望 <table></table> 中的所有列 (<td></td>) 具有相同的长度,如何实现?

android - 长按时在自定义适配器上显示轻微的色调覆盖

java - 如何在 Spring 批处理中使用 FlatFileItemWriter 写入标准输出?

java - 创建适当的 ivy 配置(而不是弄乱 *)

java - Class.forName (“com.mysql.jdbc.Driver” ) 在 Raspberry Pi 上不起作用

java - 打印一个带有可滚动 Jtable 的 JPanel

android - 在哪里可以找到微调轮教程?

java - 为什么我的自定义封闭哈希集中会发生如此多的冲突?