Results 1 to 11 of 11
- 08-27-2009, 04:06 PM #1
Member
- Join Date
- Aug 2009
- Posts
- 66
- Rep Power
- 0
- 08-27-2009, 04:16 PM #2
You might play around with the locale of NumberFormat to see if there is one for your local that has the '.' already.Java Code:java.text.NumberFormat nf = java.text.NumberFormat.getInstance(); String valString = nf.format(value).replace(",",".");My Hobby Project: LegacyClone
- 08-27-2009, 04:29 PM #3
Senior Member
- Join Date
- Aug 2009
- Posts
- 2,388
- Rep Power
- 6
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
- 08-27-2009, 04:33 PM #4
Member
- Join Date
- Aug 2009
- Posts
- 66
- Rep Power
- 0
- 08-27-2009, 04:38 PM #5
Senior Member
- Join Date
- Aug 2009
- Posts
- 2,388
- Rep Power
- 6
- 08-27-2009, 04:39 PM #6
Senior Member
- Join Date
- Aug 2009
- Posts
- 2,388
- Rep Power
- 6
-
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; } }
- 08-29-2009, 03:52 PM #8
Member
- Join Date
- Aug 2009
- Posts
- 66
- Rep Power
- 0
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 ^^
- 08-29-2009, 04:04 PM #9
Senior Member
- Join Date
- Aug 2009
- Posts
- 2,388
- Rep Power
- 6
The morale of the story is not to use Netbeans (or any IDE for that matter) when you are still learning the Java basics.
-
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....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.
- 08-30-2009, 01:15 PM #11
Member
- Join Date
- Aug 2009
- Posts
- 66
- Rep Power
- 0
finally, It was solved.
here's the number_format in Java code :
hope that this code can help others that has the same problems like me ^^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)); } }
thanks palz for your help
Similar Threads
-
Sort and filter in Jtable (java ver 1.5 )
By itaipee in forum AWT / SwingReplies: 3Last Post: 04-16-2009, 12:03 PM -
Java Swing JTable Simple Doubt
By hemanthjava in forum AWT / SwingReplies: 1Last Post: 11-26-2008, 01:46 PM -
Regarding JTable
By adeeb in forum AWT / SwingReplies: 12Last Post: 06-19-2008, 07:39 AM -
Jtable duplicates through Hashtable (JTable condition problem) my assignment plz help
By salmanpirzada1 in forum Advanced JavaReplies: 2Last Post: 05-15-2008, 10:15 AM -
Help with JTable in java
By carl in forum AWT / SwingReplies: 3Last Post: 08-11-2007, 09:47 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks