vaadin 8 grid with column of Double datatype based on Locale











up vote
0
down vote

favorite












I am trying to populate Vaadin 8 Grid and one column has the type double. I am loading the Data into the table by combining it with a bean. I did it the following way,



Tasks bean:



public class Tasks{
private int id;
private Double duration;

public StakeholderTasks(int i, String string) {
this.id = id;
this.taskName=taskName;
// TODO Auto-generated constructor stub
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public Double getDuration() {
return duration;
}
public void setDuration(Double duration) {
this.duration = duration;
}
}


TasksImpl:



public class stakeholderImpl extends CustomComponent {
private Grid < Tasks > grid1;
private Binder < Tasks > binder = new Binder < > (Tasks.class);
cmpy = datastoreinstance.updateTaskList();
provider1 = new ListDataProvider < > (cmpy);
grid1 = new Grid < > ();
grid1.addColumn(Tasks::getDuration);
grid1.asSingleSelect().addValueChangeListener(event -> {

form.setVisible(false);

window = createWindow(window,
((MainUI)(UI.getCurrent())).getLanguageMessage("general.addnewtask"),
event.getValue(), "EDIT", form);
}
catch (ClassNotFoundException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (SQLException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}

}

});

binder.forField(duration).asRequired(((MainUI)(UI.getCurrent())).getLanguageMessage("general.required"))
.withValidator(new RegexpValidator(((MainUI)(UI.getCurrent())).getLanguageMessage("general.numerror"), DBConstants.regex))
.bind(taskdetails -> nf_out.format(stakeholders.getDuration()),
(taskdetails, formValue) ->
{
NumberFormat nf_in = NumberFormat.getNumberInstance(Locale.GERMANY);
double val = 0 d;
try {
val = nf_in.parse(formValue).doubleValue();
taskdetails.setDuration(val);
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
});
}


When I click on a row in the grid, then get the values from that row, load a form and then after save, show values in the grid. What I did in the binder for duration was validate the values for double based on LOCALE.GERMANY and else validate using regex(regex= "[0-9]+([.,][0-9]{1,2})?";)



The problems I am facing are:




  • I am gettign . instead of , for decimal notations in the grid (in the database it has ,)

  • When I click on the grid, the column Duration doubles the number of zeroes. For eg: if the value in the grid is 10.0 upon clicking on it, the value changes to 10000.00 or 1000.00. Is there a way to load the grid based on Locale for Germany?


  • I know that I have not handled this correctly. Is there a better way to do this? Could someone just guide me through this. I am learning Vaadin now and I cannot get this working properly.











share|improve this question




























    up vote
    0
    down vote

    favorite












    I am trying to populate Vaadin 8 Grid and one column has the type double. I am loading the Data into the table by combining it with a bean. I did it the following way,



    Tasks bean:



    public class Tasks{
    private int id;
    private Double duration;

    public StakeholderTasks(int i, String string) {
    this.id = id;
    this.taskName=taskName;
    // TODO Auto-generated constructor stub
    }
    public int getId() {
    return id;
    }
    public void setId(int id) {
    this.id = id;
    }
    public Double getDuration() {
    return duration;
    }
    public void setDuration(Double duration) {
    this.duration = duration;
    }
    }


    TasksImpl:



    public class stakeholderImpl extends CustomComponent {
    private Grid < Tasks > grid1;
    private Binder < Tasks > binder = new Binder < > (Tasks.class);
    cmpy = datastoreinstance.updateTaskList();
    provider1 = new ListDataProvider < > (cmpy);
    grid1 = new Grid < > ();
    grid1.addColumn(Tasks::getDuration);
    grid1.asSingleSelect().addValueChangeListener(event -> {

    form.setVisible(false);

    window = createWindow(window,
    ((MainUI)(UI.getCurrent())).getLanguageMessage("general.addnewtask"),
    event.getValue(), "EDIT", form);
    }
    catch (ClassNotFoundException e1) {
    // TODO Auto-generated catch block
    e1.printStackTrace();
    } catch (SQLException e1) {
    // TODO Auto-generated catch block
    e1.printStackTrace();
    }

    }

    });

    binder.forField(duration).asRequired(((MainUI)(UI.getCurrent())).getLanguageMessage("general.required"))
    .withValidator(new RegexpValidator(((MainUI)(UI.getCurrent())).getLanguageMessage("general.numerror"), DBConstants.regex))
    .bind(taskdetails -> nf_out.format(stakeholders.getDuration()),
    (taskdetails, formValue) ->
    {
    NumberFormat nf_in = NumberFormat.getNumberInstance(Locale.GERMANY);
    double val = 0 d;
    try {
    val = nf_in.parse(formValue).doubleValue();
    taskdetails.setDuration(val);
    } catch (ParseException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }
    });
    }


    When I click on a row in the grid, then get the values from that row, load a form and then after save, show values in the grid. What I did in the binder for duration was validate the values for double based on LOCALE.GERMANY and else validate using regex(regex= "[0-9]+([.,][0-9]{1,2})?";)



    The problems I am facing are:




    • I am gettign . instead of , for decimal notations in the grid (in the database it has ,)

    • When I click on the grid, the column Duration doubles the number of zeroes. For eg: if the value in the grid is 10.0 upon clicking on it, the value changes to 10000.00 or 1000.00. Is there a way to load the grid based on Locale for Germany?


    • I know that I have not handled this correctly. Is there a better way to do this? Could someone just guide me through this. I am learning Vaadin now and I cannot get this working properly.











    share|improve this question


























      up vote
      0
      down vote

      favorite









      up vote
      0
      down vote

      favorite











      I am trying to populate Vaadin 8 Grid and one column has the type double. I am loading the Data into the table by combining it with a bean. I did it the following way,



      Tasks bean:



      public class Tasks{
      private int id;
      private Double duration;

      public StakeholderTasks(int i, String string) {
      this.id = id;
      this.taskName=taskName;
      // TODO Auto-generated constructor stub
      }
      public int getId() {
      return id;
      }
      public void setId(int id) {
      this.id = id;
      }
      public Double getDuration() {
      return duration;
      }
      public void setDuration(Double duration) {
      this.duration = duration;
      }
      }


      TasksImpl:



      public class stakeholderImpl extends CustomComponent {
      private Grid < Tasks > grid1;
      private Binder < Tasks > binder = new Binder < > (Tasks.class);
      cmpy = datastoreinstance.updateTaskList();
      provider1 = new ListDataProvider < > (cmpy);
      grid1 = new Grid < > ();
      grid1.addColumn(Tasks::getDuration);
      grid1.asSingleSelect().addValueChangeListener(event -> {

      form.setVisible(false);

      window = createWindow(window,
      ((MainUI)(UI.getCurrent())).getLanguageMessage("general.addnewtask"),
      event.getValue(), "EDIT", form);
      }
      catch (ClassNotFoundException e1) {
      // TODO Auto-generated catch block
      e1.printStackTrace();
      } catch (SQLException e1) {
      // TODO Auto-generated catch block
      e1.printStackTrace();
      }

      }

      });

      binder.forField(duration).asRequired(((MainUI)(UI.getCurrent())).getLanguageMessage("general.required"))
      .withValidator(new RegexpValidator(((MainUI)(UI.getCurrent())).getLanguageMessage("general.numerror"), DBConstants.regex))
      .bind(taskdetails -> nf_out.format(stakeholders.getDuration()),
      (taskdetails, formValue) ->
      {
      NumberFormat nf_in = NumberFormat.getNumberInstance(Locale.GERMANY);
      double val = 0 d;
      try {
      val = nf_in.parse(formValue).doubleValue();
      taskdetails.setDuration(val);
      } catch (ParseException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
      }
      });
      }


      When I click on a row in the grid, then get the values from that row, load a form and then after save, show values in the grid. What I did in the binder for duration was validate the values for double based on LOCALE.GERMANY and else validate using regex(regex= "[0-9]+([.,][0-9]{1,2})?";)



      The problems I am facing are:




      • I am gettign . instead of , for decimal notations in the grid (in the database it has ,)

      • When I click on the grid, the column Duration doubles the number of zeroes. For eg: if the value in the grid is 10.0 upon clicking on it, the value changes to 10000.00 or 1000.00. Is there a way to load the grid based on Locale for Germany?


      • I know that I have not handled this correctly. Is there a better way to do this? Could someone just guide me through this. I am learning Vaadin now and I cannot get this working properly.











      share|improve this question















      I am trying to populate Vaadin 8 Grid and one column has the type double. I am loading the Data into the table by combining it with a bean. I did it the following way,



      Tasks bean:



      public class Tasks{
      private int id;
      private Double duration;

      public StakeholderTasks(int i, String string) {
      this.id = id;
      this.taskName=taskName;
      // TODO Auto-generated constructor stub
      }
      public int getId() {
      return id;
      }
      public void setId(int id) {
      this.id = id;
      }
      public Double getDuration() {
      return duration;
      }
      public void setDuration(Double duration) {
      this.duration = duration;
      }
      }


      TasksImpl:



      public class stakeholderImpl extends CustomComponent {
      private Grid < Tasks > grid1;
      private Binder < Tasks > binder = new Binder < > (Tasks.class);
      cmpy = datastoreinstance.updateTaskList();
      provider1 = new ListDataProvider < > (cmpy);
      grid1 = new Grid < > ();
      grid1.addColumn(Tasks::getDuration);
      grid1.asSingleSelect().addValueChangeListener(event -> {

      form.setVisible(false);

      window = createWindow(window,
      ((MainUI)(UI.getCurrent())).getLanguageMessage("general.addnewtask"),
      event.getValue(), "EDIT", form);
      }
      catch (ClassNotFoundException e1) {
      // TODO Auto-generated catch block
      e1.printStackTrace();
      } catch (SQLException e1) {
      // TODO Auto-generated catch block
      e1.printStackTrace();
      }

      }

      });

      binder.forField(duration).asRequired(((MainUI)(UI.getCurrent())).getLanguageMessage("general.required"))
      .withValidator(new RegexpValidator(((MainUI)(UI.getCurrent())).getLanguageMessage("general.numerror"), DBConstants.regex))
      .bind(taskdetails -> nf_out.format(stakeholders.getDuration()),
      (taskdetails, formValue) ->
      {
      NumberFormat nf_in = NumberFormat.getNumberInstance(Locale.GERMANY);
      double val = 0 d;
      try {
      val = nf_in.parse(formValue).doubleValue();
      taskdetails.setDuration(val);
      } catch (ParseException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
      }
      });
      }


      When I click on a row in the grid, then get the values from that row, load a form and then after save, show values in the grid. What I did in the binder for duration was validate the values for double based on LOCALE.GERMANY and else validate using regex(regex= "[0-9]+([.,][0-9]{1,2})?";)



      The problems I am facing are:




      • I am gettign . instead of , for decimal notations in the grid (in the database it has ,)

      • When I click on the grid, the column Duration doubles the number of zeroes. For eg: if the value in the grid is 10.0 upon clicking on it, the value changes to 10000.00 or 1000.00. Is there a way to load the grid based on Locale for Germany?


      • I know that I have not handled this correctly. Is there a better way to do this? Could someone just guide me through this. I am learning Vaadin now and I cannot get this working properly.








      java java-8 vaadin vaadin8 vaadin-grid






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 20 at 15:20

























      asked Nov 19 at 18:28









      gopi

      63




      63
























          1 Answer
          1






          active

          oldest

          votes

















          up vote
          0
          down vote













          One alternative and simple approach would be that instead you could use the StringToDoubleConverter of the framework. By default it uses NumberFormat.getNumberInstance(locale) for number format. If you want something else than the default number format of your local, you can extend StringToDoubleConverter and override getFormat method to return customized NumberFormat.






          share|improve this answer





















            Your Answer






            StackExchange.ifUsing("editor", function () {
            StackExchange.using("externalEditor", function () {
            StackExchange.using("snippets", function () {
            StackExchange.snippets.init();
            });
            });
            }, "code-snippets");

            StackExchange.ready(function() {
            var channelOptions = {
            tags: "".split(" "),
            id: "1"
            };
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function() {
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled) {
            StackExchange.using("snippets", function() {
            createEditor();
            });
            }
            else {
            createEditor();
            }
            });

            function createEditor() {
            StackExchange.prepareEditor({
            heartbeatType: 'answer',
            convertImagesToLinks: true,
            noModals: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: 10,
            bindNavPrevention: true,
            postfix: "",
            imageUploader: {
            brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
            contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
            allowUrls: true
            },
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            });


            }
            });














            draft saved

            draft discarded


















            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53380625%2fvaadin-8-grid-with-column-of-double-datatype-based-on-locale%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown

























            1 Answer
            1






            active

            oldest

            votes








            1 Answer
            1






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes








            up vote
            0
            down vote













            One alternative and simple approach would be that instead you could use the StringToDoubleConverter of the framework. By default it uses NumberFormat.getNumberInstance(locale) for number format. If you want something else than the default number format of your local, you can extend StringToDoubleConverter and override getFormat method to return customized NumberFormat.






            share|improve this answer

























              up vote
              0
              down vote













              One alternative and simple approach would be that instead you could use the StringToDoubleConverter of the framework. By default it uses NumberFormat.getNumberInstance(locale) for number format. If you want something else than the default number format of your local, you can extend StringToDoubleConverter and override getFormat method to return customized NumberFormat.






              share|improve this answer























                up vote
                0
                down vote










                up vote
                0
                down vote









                One alternative and simple approach would be that instead you could use the StringToDoubleConverter of the framework. By default it uses NumberFormat.getNumberInstance(locale) for number format. If you want something else than the default number format of your local, you can extend StringToDoubleConverter and override getFormat method to return customized NumberFormat.






                share|improve this answer












                One alternative and simple approach would be that instead you could use the StringToDoubleConverter of the framework. By default it uses NumberFormat.getNumberInstance(locale) for number format. If you want something else than the default number format of your local, you can extend StringToDoubleConverter and override getFormat method to return customized NumberFormat.







                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Nov 25 at 8:20









                Tatu Lund

                2,241139




                2,241139






























                    draft saved

                    draft discarded




















































                    Thanks for contributing an answer to Stack Overflow!


                    • Please be sure to answer the question. Provide details and share your research!

                    But avoid



                    • Asking for help, clarification, or responding to other answers.

                    • Making statements based on opinion; back them up with references or personal experience.


                    To learn more, see our tips on writing great answers.





                    Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


                    Please pay close attention to the following guidance:


                    • Please be sure to answer the question. Provide details and share your research!

                    But avoid



                    • Asking for help, clarification, or responding to other answers.

                    • Making statements based on opinion; back them up with references or personal experience.


                    To learn more, see our tips on writing great answers.




                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function () {
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53380625%2fvaadin-8-grid-with-column-of-double-datatype-based-on-locale%23new-answer', 'question_page');
                    }
                    );

                    Post as a guest















                    Required, but never shown





















































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown

































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown







                    Popular posts from this blog

                    Paul Cézanne

                    UIScrollView CustomStickyHeader Resize height generates problems when scroll is too fast

                    Angular material date-picker (MatDatepicker) auto completes the date on focus out