按键事件不在日期选择器中工作

时间:2015-09-19 08:18:14

标签: java swing datepicker keypress keylistener

我需要Java Swing中日期选择器的按键事件。 我试过这个,但它没有用。

if (evt.getKeyCode()==evt.VK_ENTER)
{
   txttotal.requestFocus();
}

1 个答案:

答案 0 :(得分:1)

您可以在基本UI中使用与此代码相同的弹出窗口来查找内部组件。 但您必须按名称而不是常量索引查找组件。

public class MainClass {
    public static void main(String [] args) {
        JFrame jFrame=new JFrame();
        jFrame.setLayout(null);
        jFrame.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
        jFrame.setBounds(10,10,500,500);
        JInternalFrame frame=new JInternalFrame("test",false,true,false,true);
        frame.setLayout(null);
        frame.setBounds(100, 100, 200, 200);
        frame.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);

        frame.setVisible(true);
        DatePicker datePicker=new DatePicker() {

        };

        datePicker.setBounds(10,10,100,20);
        frame.add(datePicker);
        jFrame.add(frame);
        jFrame.setVisible(true);

        datePicker.showPopup();
        datePicker.hidePopup();
        BasicDatePickerUI datePickerUI= (BasicDatePickerUI )datePicker.getUI();
        datePicker.getComponent(1).addKeyListener(new KeyListener() {
            @Override
            public void keyTyped(KeyEvent e) {
                if(e.getKeyCode()==e.VK_ENTER) {
                    System.out.println("TYPE OK ON BUTTON!");
                }
            }

            @Override
            public void keyPressed(KeyEvent e) {
                if(e.getKeyCode()==e.VK_ENTER) {
                    System.out.println("PRESS OK ON BUTTON!");
                }
            }

            @Override
            public void keyReleased(KeyEvent e) {
                if(e.getKeyCode()==e.VK_ENTER) {
                    System.out.println("RELEASE OK ON BUTTON!");
                }
            }
        });

        datePicker.getComponent(0).addKeyListener(new KeyListener() {
            @Override
            public void keyTyped(KeyEvent e) {
                if(e.getKeyCode()==e.VK_ENTER) {
                    System.out.println("TYPE OK ON TEXT!");
                }
            }

            @Override
            public void keyPressed(KeyEvent e) {
                if(e.getKeyCode()==e.VK_ENTER) {
                    System.out.println("PRESS OK ON TEXT!");
                }
            }

            @Override
            public void keyReleased(KeyEvent e) {
                if(e.getKeyCode()==e.VK_ENTER) {
                    System.out.println("RELEASE OK ON TEXT!");
                }
            }
        });
        try {
            Field field=BasicDatePickerUI.class.getDeclaredField("popup");
            field.setAccessible(true);
            JPopupMenu popup=(JPopupMenu)field.get(datePickerUI);
            ((JComponent) ((JComponent) popup.getComponent(0)).getComponent(2)).addKeyListener(new KeyListener() {
                @Override
                public void keyTyped(KeyEvent e) {
                    if(e.getKeyCode()==e.VK_ENTER) {
                        System.out.println("TYPE OK ON POPUP!");
                    }

                }

                @Override
                public void keyPressed(KeyEvent e) {
                    if(e.getKeyCode()==e.VK_ENTER) {
                        System.out.println("PRESS OK ON POPUP!");
                    }

                }

                @Override
                public void keyReleased(KeyEvent e) {
                    if(e.getKeyCode()==e.VK_ENTER) {
                        System.out.println("RELEASE OK ON POPUP!");
                    }

                }
            });
            System.out.println("wait for ENTER key...");
        } catch (NoSuchFieldException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }

    }

}

如果在DatePicker文本或弹出窗口上按ENTER键,您可以在控制台输出中看到 PRESS OK ON POPUP PRESS OK ON TEXT