Results 1 to 17 of 17
  1. #1
    newbiejava is offline Senior Member
    Join Date
    Jan 2010
    Posts
    138
    Rep Power
    0

    Default How to change number format of a field programmatically in Jasper?

    Dear All,

    How to change number format of a field programmatically in Jasper?

    please help

    thanks

  2. #2
    wsaryada is offline Senior Member
    Join Date
    Jun 2007
    Location
    Bali, Indonesia
    Posts
    760
    Rep Power
    8

    Default

    You can change the format using java.text.DecimalFormat class.

  3. #3
    newbiejava is offline Senior Member
    Join Date
    Jan 2010
    Posts
    138
    Rep Power
    0

    Default

    can you please give me an example ..? thanks in advance

  4. #4
    wsaryada is offline Senior Member
    Join Date
    Jun 2007
    Location
    Bali, Indonesia
    Posts
    760
    Rep Power
    8

    Default

    You can have something like this in your Jasper template:

    Java Code:
    java.text.NumberFormat.getInstance(java.util.Locale.US).format($F{FIELD_NAME})

  5. #5
    newbiejava is offline Senior Member
    Join Date
    Jan 2010
    Posts
    138
    Rep Power
    0

    Default

    Hi..
    my case is I want to change number format of a textField in Jasper at run-time..

    ie.
    I want to change textField number format to ###,##0 (default is: ###,##0.00)

    please help
    Last edited by newbiejava; 09-02-2010 at 05:28 PM.

  6. #6
    mine0926 is offline Senior Member
    Join Date
    Apr 2010
    Location
    Philippines
    Posts
    580
    Rep Power
    5

    Default

    Why would you like to change the Number format at run-time. It is a report and should have fix format of output.

  7. #7
    newbiejava is offline Senior Member
    Join Date
    Jan 2010
    Posts
    138
    Rep Power
    0

    Default

    I give user options for changing the numericformat when they want to run report..
    the numeric format options are:

    ###,##0
    ###,##0.00

    so, the numeric format of JTextField of the report should follow to chosen numeric format option

  8. #8
    mine0926 is offline Senior Member
    Join Date
    Apr 2010
    Location
    Philippines
    Posts
    580
    Rep Power
    5

    Default

    use tertiary format.
    SYNTAX:
    Java Code:
    condition ? expression (true part): expression (false part)

  9. #9
    newbiejava is offline Senior Member
    Join Date
    Jan 2010
    Posts
    138
    Rep Power
    0

    Default

    from java class.. how to change the JTextField pattern format ?
    could you please give me an example?
    Thanks in Advance

  10. #10
    mine0926 is offline Senior Member
    Join Date
    Apr 2010
    Location
    Philippines
    Posts
    580
    Rep Power
    5

    Default

    Nope. you will do this at iReport. You will need to create a parameter and a variable. At variable you create place your conditional expression or simply use the parameter to format the variable..
    CONDITIONAL:
    Java Code:
    $P{param_input}.equals( new java.lang.String('someInput')) ? new java.text.DecimalFormat("#,##0").format($F{YOUR_FIELD}) : new java.text.DecimalFormat("#,##0.00").format($F{YOUR_FIELD})
    * OR
    USE THE PARAMETER INPUT.
    USER INPUT should be in "#,##0" or "#,##0.00" only
    Java Code:
    new java.text.DecimalFormat($P{param_input}).format($F{YOUR_FIELD})

  11. #11
    newbiejava is offline Senior Member
    Join Date
    Jan 2010
    Posts
    138
    Rep Power
    0

    Default

    I am also new to jasper, where should put the codes below:

    new java.text.DecimalFormat($P{param_input}).format($F {YOUR_FIELD}) :confused:

  12. #12
    mine0926 is offline Senior Member
    Join Date
    Apr 2010
    Location
    Philippines
    Posts
    580
    Rep Power
    5

    Default

    From the Report Inspector click the variable that you created then from its properties you will see variable expression, type the code there.

  13. #13
    newbiejava is offline Senior Member
    Join Date
    Jan 2010
    Posts
    138
    Rep Power
    0

    Default

    Thanks,
    what I have done are as follow:

    I created a parameter named NUMBER_ FORMAT
    and apply it on the field -> edit expression -> variables -> then put: new java.text.DecimalFormat($P{NUMBER_FORMAT}).format( $F{displaycost})
    and with call it thru my java codes below:

    Java Code:
    public void viewReport() {
            try {
                String jrxml = getJRXML();
                if (cls1.isFileDirectoryExists(jrxml)) {
                    
                    Map params = new HashMap();
                    params.put("NUMBER_FORMAT", "###,##0.0000"); // just for try
    
    
                    JasperDesign jasperDesign = JRXmlLoader.load(jrxml);
                    JRDesignQuery query = new JRDesignQuery();
                    query.setText(getSQL());
                    jasperDesign.setQuery(query);
                    JasperReport jasperReport = JasperCompileManager.compileReport(jasperDesign);
                    Connection jdbcConn = connectMySQL();
    
    
                    JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport, params, jdbcConn);
                    JasperViewer.viewReport(jasperPrint, false);
                } else {
                    cls1.msgBox(null, jrxml + " does not exists", Class1.MESSAGE_INFO);
                }
                
            } catch (Exception ex) {
                cls1.msgBox(null, ex.toString() , Class1.MESSAGE_INFO);
            }
        }

    I tried it but it gives me below exception:

    net.sf.jasperreports.engine.fill.JRExpressionEvalE xception: Error evaluating expression:
    Source.text: new.java.text.DecimalFormat($P{NUMBER_FORMAT}).for mat($F{displaycost})
    Last edited by newbiejava; 09-04-2010 at 03:03 AM.

  14. #14
    mine0926 is offline Senior Member
    Join Date
    Apr 2010
    Location
    Philippines
    Posts
    580
    Rep Power
    5

    Default

    Did you try to preview the report using just the iReport application?

  15. #15
    newbiejava is offline Senior Member
    Join Date
    Jan 2010
    Posts
    138
    Rep Power
    0

    Default

    strange.. it works if I try it using iReport application..
    what I missed with my codes?

  16. #16
    mine0926 is offline Senior Member
    Join Date
    Apr 2010
    Location
    Philippines
    Posts
    580
    Rep Power
    5

    Default

    Quote Originally Posted by newbiejava View Post
    Thanks,
    what I have done are as follow:

    I created a parameter named NUMBER_ FORMAT
    and apply it on the field -> edit expression -> variables -> then put: new java.text.DecimalFormat($P{NUMBER_FORMAT}).format( $F{displaycost})
    and with call it thru my java codes below:
    Hmm... I think we are having misunderstanding here....

    Did you created a parameter called NUMBER_FORMAT and paste the code in its expression editor?

    Lets check if you did these steps.

    1. Create a parameter name it NUMBER_FORMAT. (as I can see you have done this).
    2. Create a variable called New_DisplayCost.
    *From report inspector(left side of your screen) right click VARIABLES(icon is like fx)>add variables(it will create new variable called variable1)>right click variable1>rename it to New_DisplayCost.
    3. On the right side of your screen there is a properties window(if you dont see it goto Window>click Properties). From properties window you will see a VARIABLE Expression, paste the code there.

  17. #17
    newbiejava is offline Senior Member
    Join Date
    Jan 2010
    Posts
    138
    Rep Power
    0

    Default

    Thanks mine0926.. :)
    it solved!

    you saved my time.. :cool:

Similar Threads

  1. Replies: 4
    Last Post: 03-16-2012, 12:40 PM
  2. jasper report ,jasper report jar files and maven dependencies.
    By rajuchacha007 in forum Advanced Java
    Replies: 1
    Last Post: 04-09-2010, 09:15 AM
  3. Replies: 4
    Last Post: 01-18-2010, 12:13 AM
  4. How to change the format of date?
    By jboy in forum New To Java
    Replies: 1
    Last Post: 09-09-2009, 02:22 PM
  5. Close a JDialog Programmatically
    By Marcus in forum Advanced Java
    Replies: 1
    Last Post: 07-06-2007, 05:06 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •