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

    Default JTable format numbers in column

    How can I format the column in Jtable like 1,000.00?

    I tried to search at intellisense (JTable.), read them to see what might help but nothing shows.

    Thanks in advance,
    geje

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    If re-you read the Sun Swing tutorial on JTable, you'll want to pay careful attention to the section on cell renderers for this is what you want -- a custom renderer that uses a DecimalFormat object to format the numeric output as you desire.

    Have a look, give it a try, and please come back with your code if it doesn't work or if you're confused about a section. Much luck!

  3. #3
    camickr is offline Senior Member
    Join Date
    Jul 2009
    Posts
    1,236
    Rep Power
    7

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

    Default

    Thanks for the link. I use FormatRenderer.java and NumberRenderer.java to format the data in JTable cells.
    I am trying to remove locale from CurrencyInstance with no success.
    Java Code:
    	public static NumberRenderer getCurrencyRenderer()
    	{       [b]Locale l = new Locale("", "", "");[/b] //I add this
    		return new NumberRenderer(NumberFormat.getCurrencyInstance(l));
    	}

    So I try to use DecimalFormat..
    Java Code:
    	public static NumberRenderer getDecimalRenderer()
    	{   DecimalFormat df = new DecimalFormat("#,##0.00");
                return new NumberRenderer(df);
    	}

    I am actually trying things here, so I am not sure if this is the right thing to do.
    Is there any way to do this?

    Thanks for guiding me,
    geje

  5. #5
    camickr is offline Senior Member
    Join Date
    Jul 2009
    Posts
    1,236
    Rep Power
    7

    Default

    I am trying to remove locale from CurrencyInstance
    I don't know what that means.

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

    Default

    When I call NumberFormat.getCurrencyRenderer it returns $1,000.00. I would like to remove the dollar sign($).

    Sorry for the bad english. :D

  7. #7
    camickr is offline Senior Member
    Join Date
    Jul 2009
    Posts
    1,236
    Rep Power
    7

    Default

    Your second example works fine for me:
    Java Code:
    NumberRenderer nr = new NumberRenderer(new DecimalFormat("#,##0.00"));
    tcm.getColumn(3).setCellRenderer(nr);

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

Similar Threads

  1. JCheckbox in a Jtable Column
    By kedia_rohit in forum AWT / Swing
    Replies: 1
    Last Post: 04-25-2010, 01:45 PM
  2. Format Numbers
    By ŖΫ ỏ Ңόρę in forum New To Java
    Replies: 2
    Last Post: 03-07-2010, 08:57 PM
  3. split jtable column
    By anilkumar_vist in forum Advanced Java
    Replies: 3
    Last Post: 11-30-2009, 03:14 PM
  4. JCheckBox in JTable column
    By hind in forum New To Java
    Replies: 8
    Last Post: 01-04-2009, 08:40 PM
  5. How to sort column in JTable
    By johnt in forum AWT / Swing
    Replies: 3
    Last Post: 06-14-2008, 06:48 AM

Posting Permissions

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