Menu

#5 CalendarGridPanel, mouse tracking

open
nobody
5
2014-03-18
2012-07-06
Anonymous
No

It seems to me the component looks nicer if it tracks the mouse movements and is easy to support.

CalendarGridPanel constructor:
- // addMouseListener(this);
+ addMouseListener(new MouseAdapter() {
+ public void mouseEntered(MouseEvent e) {
+ requestFocusInWindow();
+ }
+ });

public void mouseEntered(MouseEvent e) {
+ Component c = e.getComponent();
+ if (c instanceof DateLabel) {
+ try {
+ int day = Integer.parseInt(((DateLabel) c).getText());
+ Calendar cal = Calendar.getInstance();
+ cal.setTime(focusDate);
+ cal.set(Calendar.DAY_OF_MONTH, day);
+ focusDate = cal.getTime();
+ reflectData();
+ } catch (NumberFormatException ex) {}
+ }
}

The mouseEntered above does the job, but probably there's a simpler version that reads the focusDate directly from DateLabel. Unfortunately, this didn't work for me and I ran out of time do to further exploration. The basic idea would be something like:
// !!!The following impl. does not work because DateLabel.getDate() always returns null !!!
public void mouseEntered(MouseEvent e) {
Component c = e.getComponent();
if (c instanceof DateLabel) {
requestFocusInWindow();
Date d = ((DateLabel) c).getDate();
if (d != null) {
focusDate = d;
reflectData();
}
}
}

Discussion

  • Anonymous

    Anonymous - 2012-07-06

    expanding on the idea... one could add a flag (boolean) to DatePicker for enabling/disabling the mouse tracking feature, and it would propagate all the way to CalendarGridPanel (property change events, peer.get..., etc.)

     
  • Anonymous

    Anonymous - 2012-07-06

    one little fix to the working mouseEntered: it also needs a call to requestFocusInWindow(); right after "if (c instanceof DateLabel) {"

     
  • teddybear

    teddybear - 2014-03-18
     

Log in to post a comment.