Results 1 to 11 of 11
  1. #1
    sysout is offline Member
    Join Date
    Aug 2009
    Posts
    66
    Rep Power
    0

    Default [ resolved ] number_format() and JTable in java

    hai palz, how to convert 1000000 to : 1.000.000 ?

    And how to change JTable width and Center the rows? :D
    sorry for the questions and thank you very much for your answer.

    thanksss palz..;)
    Last edited by sysout; 08-30-2009 at 01:49 PM.

  2. #2
    mrmatt1111's Avatar
    mrmatt1111 is offline Senior Member
    Join Date
    Aug 2009
    Location
    San Jose, CA, USA
    Posts
    320
    Rep Power
    5

    Default

    Java Code:
    java.text.NumberFormat nf = java.text.NumberFormat.getInstance();
    
    String valString = nf.format(value).replace(",",".");
    You might play around with the locale of NumberFormat to see if there is one for your local that has the '.' already.
    My Hobby Project: LegacyClone

  3. #3
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    7

    Default

    Quote Originally Posted by sysout View Post
    hai palz, how to convert 1000000 to : 1.000.000 ?

    And how to change JTable width and Center the rows? :D
    sorry for the questions and thank you very much for your answer.

    thanksss palz..;)
    For the width, how about checking the API specs for the classes you are using for the tables to see what methods are available.

    The formatting is much easier but you'll need to be specific. Is that the only conversion you want to do or was that just an example? Are decimal point numbers allowed? e.t.c

  4. #4
    sysout is offline Member
    Join Date
    Aug 2009
    Posts
    66
    Rep Power
    0

    Default

    Quote Originally Posted by mrmatt1111 View Post
    Java Code:
    java.text.NumberFormat nf = java.text.NumberFormat.getInstance();
    
    String valString = nf.format(value).replace(",",".");
    You might play around with the locale of NumberFormat to see if there is one for your local that has the '.' already.
    any full examples palz?

    int A = 1000000;
    String B;

    B -> can be 1.000.000 ?
    any code to implement this? (sorry I'm newbie)

  5. #5
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    7

    Default

    Quote Originally Posted by mrmatt1111 View Post
    Java Code:
    java.text.NumberFormat nf = java.text.NumberFormat.getInstance();
    
    String valString = nf.format(value).replace(",",".");
    You might play around with the locale of NumberFormat to see if there is one for your local that has the '.' already.
    What do you suppose that will do?

  6. #6
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    7

    Default

    Quote Originally Posted by sysout View Post
    any full examples palz?

    int A = 1000000;
    String B;

    B -> can be 1.000.000 ?
    any code to implement this? (sorry I'm newbie)
    Read my response above again.

  7. #7
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    If you are trying to format the data shown in your JTable you'll probably want to create a table cell renderer that uses a NumberFormat that uses the proper locale. To learn how to do that check out this: How to Use Tables (The Java™ Tutorials > Creating a GUI with JFC/Swing > Using Swing Components)

    Here's a simple example:
    Java Code:
    import java.text.NumberFormat;
    import java.util.Locale;
    
    import javax.swing.JOptionPane;
    import javax.swing.JScrollPane;
    import javax.swing.JTable;
    import javax.swing.table.DefaultTableCellRenderer;
    import javax.swing.table.DefaultTableModel;
    import javax.swing.table.TableCellRenderer;
    
    public class NumberFormatFun {
      public static void main(String[] args) {
        final NumberFormat usNF = NumberFormat
            .getInstance(Locale.US);
        final NumberFormat germanyNF = NumberFormat
            .getInstance(Locale.GERMANY);
    
        Integer[][] data = { { 1000000, 2000000, 3000000 },
            { 1515151, 2525252, 3535353 } };
        String[] header = { "A", "B", "C" };
    
        DefaultTableModel model = new DefaultTableModel(data,
            header);
    
        JTable table = new JTable(model);
    
        JTable usTable = new LocaleTable(model, usNF); 
        JTable germanyTable = new LocaleTable(model, germanyNF);
    
        JOptionPane.showMessageDialog(null, new JScrollPane(
            table), "No Formatting", JOptionPane.PLAIN_MESSAGE);
        JOptionPane.showMessageDialog(null, new JScrollPane(
            usTable), "US Formatting", JOptionPane.PLAIN_MESSAGE);
        JOptionPane.showMessageDialog(null, new JScrollPane(
            germanyTable), "Germany Formatting", JOptionPane.PLAIN_MESSAGE);
    
      }
    }
    
    class LocaleRenderer extends DefaultTableCellRenderer {
      NumberFormat numberFormat;
    
      public LocaleRenderer(NumberFormat nf) {
        super();
        this.numberFormat = nf;
      }
    
      public void setValue(Object value) {
        setText((value == null) ? "" : numberFormat
            .format(value));
      }
    }
    
    class LocaleTable extends JTable {
      private TableCellRenderer renderer;
    
      public LocaleTable(DefaultTableModel model, NumberFormat format) {
        super(model);
        renderer = new LocaleRenderer(format);
      }
      
      public TableCellRenderer getCellRenderer(int row,
          int column) {
        return renderer;
      }
    }

  8. #8
    sysout is offline Member
    Join Date
    Aug 2009
    Posts
    66
    Rep Power
    0

    Default

    great, that's absolutely works palz, but I don't understand how to combine with my code.
    I'm newbie, my JTable was automatically created by Netbeans, even the code can't be edited.

    BuatPemesanan.java - FileFactory

    that's my code pals ^^

  9. #9
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    7

    Default

    The morale of the story is not to use Netbeans (or any IDE for that matter) when you are still learning the Java basics.

  10. #10
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    ...but I don't understand how to combine with my code.
    I'm newbie, my JTable was automatically created by Netbeans, even the code can't be edited.
    Then you may be Borked. Your options are: either see if NetBeans will allow you to modify your JTable to accept custom cell renderers or do as suggested above (and as I heartily second), do your Swing coding by hand.

  11. #11
    sysout is offline Member
    Join Date
    Aug 2009
    Posts
    66
    Rep Power
    0

    Default

    finally, It was solved.
    here's the number_format in Java code :

    Java Code:
    import java.text.NumberFormat;
    import java.util.Locale;
    
    
    public class trial {
      public static void main(String[] args) {
        float amount = (float) 54321.85;
        NumberFormat usFormat = NumberFormat.getIntegerInstance(Locale.US);
        System.out.println(usFormat.format(amount));
        NumberFormat germanFormat = NumberFormat.getIntegerInstance(Locale.GERMANY);
        System.out.println(germanFormat.format(amount));
      }
    }
    hope that this code can help others that has the same problems like me ^^
    thanks palz for your help

Similar Threads

  1. Sort and filter in Jtable (java ver 1.5 )
    By itaipee in forum AWT / Swing
    Replies: 3
    Last Post: 04-16-2009, 12:03 PM
  2. Java Swing JTable Simple Doubt
    By hemanthjava in forum AWT / Swing
    Replies: 1
    Last Post: 11-26-2008, 01:46 PM
  3. Regarding JTable
    By adeeb in forum AWT / Swing
    Replies: 12
    Last Post: 06-19-2008, 07:39 AM
  4. Replies: 2
    Last Post: 05-15-2008, 10:15 AM
  5. Help with JTable in java
    By carl in forum AWT / Swing
    Replies: 3
    Last Post: 08-11-2007, 09:47 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
  •