为什么JDateChooser不会收到焦点事件

时间:2014-04-10 21:29:40

标签: java swing jdatechooser focuslistener

我正在尝试将FocusAdapter添加到JDateChooser摇摆项目,但focusGained()方法未触发,无论是通过标签焦点还是鼠标单击... < / p>

public static void main(String[] args) {
    JPanel panel = new JPanel(new GridBagLayout());
    GridBagConstraints c = new GridBagConstraints();
    c.fill = GridBagConstraints.HORIZONTAL;
    c.weightx = 1;

    JTextField textField = new JTextField();
    panel.add(textField, c);
    JDateChooser dateChooser = new JDateChooser(new Date());
    dateChooser.addFocusListener(new FocusAdapter() {
        @Override
        public void focusGained(FocusEvent evt) {
            System.out.println(evt.getSource()); // This line never runs
        }
    });

    c.gridy = 1;
    panel.add(dateChooser, c);

    JFrame frame = new JFrame();
    frame.add(panel);
    frame.pack();
    frame.setLocationRelativeTo(null);
    frame.setVisible(true);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}

令人沮丧......我错过了一些小事吗?

1 个答案:

答案 0 :(得分:3)

基于JavaDocs,您需要获取充当JDateChooser

编辑器的UI组件
JDateChooser dateChooser = new JDateChooser(new Date());
IDateEditor editor = dateChooser.getDateEditor();
JComponent comp = editor.getUiComponent();
comp.addFocusListener(...);