1

I am trying to extend JXDatePicker so that it opens up when it gains focus. Have searched for suggest that I understand without success. Is there an elegant way of doing this?

mKorbel
  • 109,525
  • 20
  • 134
  • 319
Tony Wolff
  • 732
  • 1
  • 10
  • 23
  • as always with JSomething: don't extend :-) And as always with compound components: registering low-level listeners must be done on the relevant child, not the parent (same as with a editable combo) – kleopatra Mar 18 '13 at 22:59
  • I am pretty new to Java is there somewhere I can get this sort of advice. In particular to find out why one shouldn't extend a JSomething, seemed quite a good idea and I have been doing it for a number of other components with apparent success :-) What disaster lies ahead for me? – Tony Wolff Mar 19 '13 at 06:38
  • @kleopatra aaaach please :-) this answer don't talking something about Whats Action showing JXxxXxxPopup with JXMonthPanel :-) – mKorbel Mar 19 '13 at 08:20
  • it's plain ol' OO basics :-) @mKorbel ehh .. what are trying to tell me? – kleopatra Mar 19 '13 at 08:44
  • @kleopatra ............... :-) – mKorbel Mar 19 '13 at 08:50

2 Answers2

3

Astonishingly, it's not really possible :-(

For once, the JXDatePicker itself has no api to show/hide the popup (only BasicDatePickerUI has). Plus the ui delegate has some internal magic (read: hacks ... cough) that makes a FocusListener even worse to handle than usually in compound components.

A snippet to play with:

final JXDatePicker picker = new JXDatePicker();
FocusListener l = new FocusListener() {

    @Override
    public void focusGained(FocusEvent e) {
        // no api on the picker,  need to use the ui delegate
        BasicDatePickerUI pickerUI = (BasicDatePickerUI) picker.getUI();
        if (!pickerUI.isPopupVisible()) {
            pickerUI.toggleShowPopup();
        }
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                // opening the popup moves the focus to ... ? 
                // need to grab it back onto the editor
                picker.getEditor().requestFocusInWindow();
            }
        });
    }

    @Override
    public void focusLost(FocusEvent e) {
    }
};
// need to register the listener on the editor
picker.getEditor().addFocusListener(l);
JComponent content = new JPanel();
content.add(new JButton("dummy"));
content.add(picker);

Not really satisfying, as automatic closing of the popup on transfering the focus out again doesn't work reliably, needs two tabs (don't know why)

kleopatra
  • 51,061
  • 28
  • 99
  • 211
0

I had the same problem. This worked for me:

jXDatePicker.getEditor().addFocusListener(new FocusListener() {
    @Override
    public void focusGained(FocusEvent e) {
        BasicDatePickerUI pickerUI = (BasicDatePickerUI) jXDatePicker.getUI();
        if (!pickerUI.isPopupVisible() && e.getOppositeComponent() != getRootPane() && e.getOppositeComponent() != jXDatePicker.getMonthView()) {
            pickerUI.toggleShowPopup();
        }
    }
    @Override
    public void focusLost(FocusEvent e) {}
});

This piece of code is used to avoid focus issues:

 e.getOppositeComponent() != getRootPane()
morlic
  • 1
  • 2