I'm trying to use the JDateChooser control from that library, and I need to add a FocusListener to the control. I added one using the addFocusListener() method as you would expect, and it's not working. The FocusListener is never even called when the control gets focus.
For example, take the following tester class:
package jcalendartest;
import com.toedter.calendar.JDateChooser;
import java.awt.BorderLayout;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;
import javax.swing.JFrame;
import javax.swing.JTextField;
public class MyJFrame extends JFrame {
public static void main(String[] args) {
MyJFrame app = new MyJFrame();
app.initializeWindow();
}
private void initializeWindow() {
this.setDefaultCloseOperation(DISPOSE_ON_CLOSE);
JDateChooser dateChooser = new JDateChooser();
dateChooser.addFocusListener(new MyFocusListener());
this.add(dateChooser, BorderLayout.NORTH);
JTextField textField = new JTextField();
textField.addFocusListener(new MyFocusListener());
this.add(textField, BorderLayout.SOUTH);
this.pack();
this.setVisible(true);
}
private class MyFocusListener implements FocusListener {
public void focusGained(FocusEvent e) {
String source = e.getComponent().getClass().getName();
System.out.println(source + " got focus");
}
public void focusLost(FocusEvent e) {}
}
}
If you compile and run that program, you'll find that when the text field gets the focus, the console writes out, "javax.swing.JTextField got focus", as expected.
On the other hand, when the JDateChooser gets focus, it should write out, "com.toedter.calendar.JDateChooser got focus", however, it doesn't. Nothing happens. Any ideas why not?


Sign In
Create Account


Back to top









