I must say that i am not new in java programming, but this code is stressing me and i apparently cant figure it out:
I have a calendar class that listens to date range selections. the design of the calendar allows one to specify
a RangeListener that is notified of range selection events.
Also, the range calendar allows you to add an item in its JPopupMenu, so that it can be displayed when a range is selected
When a range is added, the range calendar invokes the RangeListener's method rangeAdded(DateRange, PopupController).
The PopupController allows you to add an item to the date range calendar.
I define my range listener as follows
public void rangeAdded(final DateRange range, PopupController notifier) { if (this.rooms == null || this.rooms.isEmpty()) { return; } if (item == null) { item = new JMenuItem("Make Reservation: "); notifier.getPopupMenu().add(new JSeparator()); notifier.getPopupMenu().add(item); item.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { Reservation reservation = new Reservation(); reservation.setExpectedCheckinDate(range.getStartDate()); reservation.setExpectedCheckoutDate(range.getEndDate()); ReservationDialog.guestReservation(reservation); } }); } }
my problem is that the DateRange prints out to the corrent value, but the action listener receives only the first DateRange supplied when creating the JMenuItem, why cant it be able
to subsequently receive the next ranges?
I modified the code and specified the containing class as the ActionListener
public class MyRangeListener implements RangeListener, ActionListener{ private DateRange myRange; public void actionPerformed(ActionEvent e) { if (this.selectedRange != null) { //start off a reservation here Reservation reservation = new Reservation(); reservation.setExpectedCheckinDate(selectedRange.getStartDate()); reservation.setExpectedCheckoutDate(selectedRange.getEndDate()); reservation.setReservationDate(VSimpleDate.getInstance()); ReservationDialog.guestReservation(reservation); } else { System.out.println("Range null"); } } public void rangeAdded(final DateRange range, PopupController notifier) { if (this.rooms == null || this.rooms.isEmpty()) { return; } this.selectedRange = range; if (item == null) { item = new JMenuItem("Make Reservation: "); notifier.getPopupMenu().add(new JSeparator()); notifier.getPopupMenu().add(item); item.addActionListener(this); } } }
However, the second style always prints that the selected range is null?
Can someone help me please?