0

I am checking for a change in value of a date. The ValueChangeHandler is recognising a date (e.g. 1/5/2014 is updated to the DB when entered). However, when I delete a date it is not recognised (i.e., the DB is not updated to null - I have tried Backspace, highlight and Del, overtyping with spaces). I then entered a new date (2/5/2014) and this was updated to the DB. Any ideas as to why this code does not recognise that I have removed the date please.

Regards,

Glyn

I have updated this with the code suggested by Braj. Unfortunately this did not work.

final DateBox awardedDate = new DateBox();
awardedDate.setFormat(new DefaultFormat(DateTimeFormat.getFormat("dd/MM/yyyy")));
awardedDate.setValue(ymAwards.getCaAwardedDate());
awardedDate.setWidth("75px");

//Add change handler for the awarded date.
//Only a Leader or Administrator can update the date
if (accountLevel.equals("Leader") || accountLevel.equals("Administrator")) {
    awardedDate.addValueChangeHandler(new ValueChangeHandler<java.util.Date>() {
        int pog = 0;
        public void onValueChange(ValueChangeEvent<java.util.Date> event) {
            if (pog == 0) {
                pog++;
                Window.alert("First change hadler.");

                //Check for a null date and handle it for dateBoxArchived and dateBoxPackOut
                java.sql.Date sqlDateAwarded = awardedDate.getValue() == null ? null : new java.sql.Date(awardedDate.getValue().getTime());
                AsyncCallback<YMAwards> callback = new YMAwardedDateHandler<YMAwards>();
                rpc.updateYMAwarded(youthMemberID, returnAwID, sqlDateAwarded, callback);
            }else{
                pog = 0;
            }
        }
    });
    awardedDate.getTextBox().addValueChangeHandler(new ValueChangeHandler<String>() {
        @Override
        public void onValueChange(ValueChangeEvent<String> event) {
            if (event.getValue() == null) {
                Window.alert("Second change hadler.");
                //Check for a null date and handle it for dateBoxArchived and dateBoxPackOut
                java.sql.Date sqlDateAwarded = awardedDate.getValue() == null ? null : new java.sql.Date(awardedDate.getValue().getTime());
                AsyncCallback<YMAwards> callback = new YMAwardedDateHandler<YMAwards>();
                rpc.updateYMAwarded(youthMemberID, returnAwID, sqlDateAwarded, callback);
            }
        }
    });
}
Glyn
  • 1,933
  • 5
  • 37
  • 60
  • Have you confirmed that onValueChange method does not fire? – Andrei Volgin May 01 '14 at 14:32
  • Hi Andrei, Yes I placed a number of alerts around the code so I could track what was happening. Regards, Glyn. – Glyn May 01 '14 at 21:45
  • Hi Andrei, awardDate is the date the Youth Member was awarded the Award. If it has been awarded then I display this date. If it has not been awarded then it is null. A 'Leader' or 'Administrator' can then update the field entering the date awarded. If they make a mistake then they need to be able to remove the date. I have added how I define awardDate above. Regards, Glyn. – Glyn May 01 '14 at 22:19

2 Answers2

1

Add this line:

awardDate.setFireNullValues(true);

This was added in GWT 2.5.

Andrei Volgin
  • 40,755
  • 6
  • 49
  • 58
  • Hi Andrei, This works if I delete the date and then click outside of the box. However, if I delete the date and then tab out of the box the change is not fired! Any ideas please (besides - "don't tab" :-). Thank you very much for your help. Regards, Glyn. – Glyn May 01 '14 at 23:20
  • You can add BlurHandler to the DateBox, and make both BlurHandler and ValueChangeHandler execute the same private method. – Andrei Volgin May 02 '14 at 00:34
  • Hi Andrei, I could not get BlurHandler to work for the DateBox. However, after much experimenting I found that the issue has something to do with the fact that when you change the date via the date picker the onValueChange fires twice and only fires once when a space or blank is entered. To get round this add "|| (awardedDate.getValue() == null)" so we have "if ((pog == 0) || (awardedDate.getValue() == null)) {". This would have to be a common problem so I hope this explanation helps everyone else. Thank you very much for your help. Regards, Glyn. – Glyn May 02 '14 at 05:49
  • Hi Andrei, I just did some more testing. The second and subsequent times you manually enter the date, without using the date picker or entering space/null in between, it is not recognised. I am not sure how to resolve this however it should be a rare occurrence. I will post back here if I can find a resolution. Any thoughts from anyone else greatly appreciated. Regards, Glyn – Glyn May 02 '14 at 06:10
  • Take a look at this issue, if you have not done it already: https://code.google.com/p/google-web-toolkit/issues/detail?id=4785&can=1&q=datepicker%20twice&colspec=ID%20Type%20Status%20Owner%20Milestone%20Summary%20Stars – Andrei Volgin May 02 '14 at 06:34
  • Thanks Andrei, so from this it looks like all I need to do is upgrade to the latest GWT 2.6 and it should work and I can simplify the code by removing the 'pog' checks. Unfortunately I have not had much luck with upgrades so I will need to do a full back up first. I have been working on this project for 16 months (in my spare time) and once this has been resolved I will be uploading it to the Group's servers. So I do not want anything to go wrong. Thanks for your help. Regards, Glyn. – Glyn May 03 '14 at 23:52
  • Hi Andrei, (and any interested peruser), I can confirm that GWT 2.6 and gwtupload-1.0.1.jar fix this issue. Please see "http://stackoverflow.com/questions/23455516/issue-with-instalation-of-gwt-2-6/23455886?iemail=1&noredirect=1#23455886" for details. Thanks and regards, Glyn. – Glyn May 05 '14 at 03:21
0

Try this one also

    final DateBox dateBox = new DateBox();

    dateBox.getTextBox().addValueChangeHandler(new ValueChangeHandler<String>() {

        @Override
        public void onValueChange(ValueChangeEvent<String> event) {
            if (dateBox.getValue() == null) {
                System.out.println("date value is empty");
                // your code here
            }
        }
    });

output:

date value is empty

DateBox#addValueChangeHandler() fires when there is any change in date via date picker.

You can check the value in text box using TextBox#addValueChangeHandler().

Braj
  • 46,415
  • 5
  • 60
  • 76
  • Hi Braj, thank you for your effort. However, unfortunately this did not work. Neither of the alerts fire when I blank out the awardedDate. – Glyn May 01 '14 at 22:12
  • @Glyn Sorry typo mistake. use `dateBox.getValue()` instead of `event.getValue()` – Braj May 02 '14 at 02:49
  • Hi Braj, Thank you. I have this now working by using the "awardDate.setFireNullValues(true);" from above and changing the "if" statement (please see explanation above). My only issue now is as per my last comment to Andrei. Any ideas? Regards, Glyn. – Glyn May 02 '14 at 06:19