Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7058401
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T04:06:30+00:00 2026-05-28T04:06:30+00:00

We need to support high precision for certain numbers (interest rates) in an application,

  • 0

We need to support high precision for certain numbers (interest rates) in an application, but we’d still like to show them rounded to 4 decimal places by default.

So, in a table, some cells contain rates in a TextField<BigDecimal> where user can enter a value with any precision, but which shows the value with 4 decimal points. This is done with a custom BigDecimal converter (as documented in this answer).

We also have a simple tooltip (title attribute) in place, showing the exact value (when I say “exact”, there’s of course some upper limit to the scale, currently 16):

enter image description here

These fields have “onblur” AjaxFormComponentUpdatingBehavior, which causes changed value to be immediately saved and the rest of the table updated.

Now, there are two problems:

  1. If user clicks the field, then clicks somewhere else, the rounded (4
    decimal) value gets updated in domain object & persisted without the
    user even realising she changed anything. I.e., the exact value gets lost.
  2. Somewhat trickier: I’d like to make editing the exact value easy.
    I.e., when the textfield is clicked (focused), it should change to
    show the exact value so user can easily edit that
    (and not the
    rounded value).

As a hacky fix to #1 I changed the setter of the underlying DTO so that it doesn’t actually change the value if the incoming new value is the same but with smaller scale. But I’m looking for something that solves #2 (which would automatically take care of #1 too).

final TextField<BigDecimal> field;
BigDecimal interestRate = ...; // current exact value from the model object

field.add(new AjaxFormComponentUpdatingBehavior("onfocus") {
    @Override
    protected void onUpdate(AjaxRequestTarget target) {
        // something I could do here? using e.g. 
        // field.setModelObject(), field.setConvertedInput() 
        // or field.getConverter(BigDecimal.class);
      }
 }

Having the field, the domain object & the exact BigDecimal value at hand as in above code (executed on every update of the table), what could I do to replace the value visible (editable) in UI?

Could I somehow swap the converter when the field is focused? There isn’t a setter for converter, but perhaps create a custom TextField subclass that allows you to change the converter on the fly…?

As last resort we could change the converter (increase setMaximumFractionDigits) so that the exact values would always be displayed (but that isn’t a solution to this specific problem, it’s merely sidestepping it).

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T04:06:31+00:00Added an answer on May 28, 2026 at 4:06 am

    Well, I got it working eventually. Not using pure Wicket, but with a small JavaScript hack.

    As mentioned in the question, I already had the exact value in the title attribute of the field. So I can just copy the attribute value as the input field’s value.

    Java:

    For these kinds of TextFields, add a Behavior which calls a JS function. Essential method here is getMarkupId() which returns the (arbitrary, dynamic) <input> element ID that I can use in JS.

    private void addPreservePrecisionBehavior(final TextField<BigDecimal> field) {
        field.add(new AjaxFormComponentUpdatingBehavior("onclick") {
            @Override
            protected void onUpdate(AjaxRequestTarget target) {
                String js = String.format("copyExactValueFromTitle('%s')", field.getMarkupId());
                target.appendJavascript(js);
            }
        });
    }
    

    JavaScript (in a .js file which gets loaded on the relevant Wicket page):

    /**
     * Takes the specified input field's title and sets it as the field's value.
     *
     * @param elementId ID of an input element
     */
    function copyExactValueFromTitle(elementId) {
        if (!elementId) {
            return;
        }
        var input = $('#' + elementId);
        var exactValue = input.attr("title");
        if (exactValue) {
            input.val(exactValue);
        }
    }
    

    (Note that jQuery is used above; you could of course do this without it too.)

    The only problem with this is, at least in our case, a slight delay (maybe 200-500 ms) before the onUpdate() method gets called, which makes editing the field a little less smooth. (If user changes the value quickly, that change gets overridden by the JS code.)

    To minimise that annoyance, I only add the Behavior when it is really needed:

     if (interestRate != null && interestRate.stripTrailingZeros().scale() > 4) {
         addPreservePrecisionBehavior(field);
     }
    

    Feel free to post better solutions!

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to support Chinese in one of my application on Windows, the problem
I just started using jqModal as I need support for nested modals. I'm noticing
Since i'm a poor sql developer, i need support to write a sql query
I need to support exact phrases (enclosed in quotes) in an otherwise space-separated list
In a current project I need to support finding a User by login credentials
I have been given a requirement where I need to support multiple databases in
I am creating a multilingual site that will need to support at minimum five
For my Swing project, I need to support both Java 5 and Java 6
I'm working on a text editor in ruby, and I need to support a
We need to decide between Support Vector Machines and Fast Artificial Neural Network for

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.