我正在尝试将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);
}
令人沮丧......我错过了一些小事吗?
答案 0 :(得分:3)
基于JavaDocs,您需要获取充当JDateChooser
JDateChooser dateChooser = new JDateChooser(new Date());
IDateEditor editor = dateChooser.getDateEditor();
JComponent comp = editor.getUiComponent();
comp.addFocusListener(...);