Search...
FAQs
Subscribe
Pie
FAQs
Recent topics
Flagged topics
Hot topics
Best topics
Search...
Search within Swing / AWT / SWT
Search Coderanch
Advance search
Google search
Register / Login
Post Reply
Bookmark Topic
Watch Topic
New Topic
programming forums
Java
Mobile
Certification
Databases
Caching
Books
Engineering
Micro Controllers
OS
Languages
Paradigms
IDEs
Build Tools
Frameworks
Application Servers
Open Source
This Site
Careers
Other
Pie Elite
all forums
this forum made possible by our volunteer staff, including ...
Marshals:
Campbell Ritchie
Jeanne Boyarsky
Ron McLeod
Paul Clapham
Liutauras Vilda
Sheriffs:
paul wheaton
Rob Spoor
Devaka Cooray
Saloon Keepers:
Stephan van Hulst
Tim Holloway
Carey Brown
Frits Walraven
Tim Moores
Bartenders:
Mikalai Zaikin
Forum:
Swing / AWT / SWT
DecimalFormat error
Mike Lipay
Ranch Hand
Posts: 171
posted 13 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
I'm not understanding why, but mf.format works while cf.format throws an error.
import java.awt.*; import java.awt.event.*; import java.io.*; import java.lang.Integer; import java.text.*; import java.util.*; import java.util.Locale; import java.util.prefs.*; import javax.swing.*; import javax.swing.JComponent.*; import javax.swing.table.*; /** * Service Form * @author Mike Lipay * @version 1.0 */ public class Service { public static void Service (JFrame frame, String arg) { displayMenu(frame, arg); } /** * Display main window to the user */ public static void displayMenu(JFrame aFrame, String arg) { final String VIN = arg; final JFrame frame = aFrame; EventQueue.invokeLater (new Runnable() { public void run() { ServiceDialog dialog = new ServiceDialog(frame, VIN); dialog.setDefaultCloseOperation (JFrame.DISPOSE_ON_CLOSE); dialog.setVisible(true); } }); } } /** * Create the window frame */ class ServiceDialog extends JDialog implements Serializable { VIN VINarray = new VIN(); static String VINrecord[]; static String VIN; static int VINindex; final String fieldSep = "\t"; final int windowWidth = 450; final int windowHeight = 350; private String[] columnNames = { "Mileage", "Service Date", "Service", "Parts", "Cost" }; static Object[][] serviceTable = { { 10000, "12/25/2008", "Engine Replacement", "Engine, hoses, fluids", 2453.85 } }; // Main panel fields private JPanel servicePanel = new JPanel(); private JTextField textFld = new JTextField(); // Work Fields DecimalFormat mf = new DecimalFormat ("000,000"); DecimalFormat cf = new DecimalFormat ("ยค000,000.00"); double dblYtdCost = 0.00; double dblTotalCost = 0.00; double dblAnnualCost = 0.00; // Text fields JTextField mileageTxtFld = new JTextField(); JTextField dateTxtFld = new JTextField(); JTextField descriptionTxtFld = new JTextField(); JTextField costTxtFld = new JTextField(); JTextField documentTxtFld = new JTextField(); /** * Create the main frame which enables the selection of or addition of * vehicles to the Vehicle Identification File (VIF.obj) */ public ServiceDialog(JFrame owner, String arg) { super (owner, "Vehicle Specifications", true); int panelLine = 0; // Main window setup setLocationByPlatform(true); setSize(windowWidth, windowHeight); setTitle("Vehicle Service"); GridBagLayout layout = new GridBagLayout(); setLayout(layout); // Get the VIN String [] tempArgs = arg.split(","); VIN = tempArgs[0]; // Read the Service record String temp = VINarray.readService(VIN); if (temp != null) { VINrecord = temp.split(fieldSep); VINindex = Integer.parseInt(VINrecord[0]); } else { VINindex = -1; } // Vehicle Information add (new JLabel (VIN), new GBC(0,panelLine,8,1).setAnchor(GBC.CENTER)); // Show Costs NumberFormat cf = NumberFormat.getCurrencyInstance(); add (new JLabel (" Current Year: "), new GBC(0,++panelLine,1,1).setAnchor(GBC.EAST)); add (new JLabel (cf.format(dblYtdCost)), new GBC(1,panelLine,1,1).setAnchor(GBC.EAST)); add (new JLabel (" Total Cost:"), new GBC(0,++panelLine,1,1).setAnchor(GBC.EAST)); add (new JLabel (cf.format(dblTotalCost)), new GBC(1,panelLine,1,1).setAnchor(GBC.EAST)); add (new JLabel (" Annual Cost.:"), new GBC(0,++panelLine,1,1).setAnchor(GBC.EAST)); add (new JLabel (cf.format(dblAnnualCost)), new GBC(1,panelLine,1,1).setAnchor(GBC.EAST)); // Blank Line add (new JLabel (" "), new GBC(0,++panelLine,3,1).setAnchor(GBC.WEST)); // Service Table TableModel model = new ServiceTableModel(1, 5); JTable table = new JTable(model); add (new JScrollPane(table), new GBC(0,++panelLine,3,1).setFill(GBC.BOTH).setWeight(100, 100)); panelLine = panelLine + 4; table.setAutoCreateRowSorter(true); table.setFillsViewportHeight(true); } // Service Table Model class ServiceTableModel extends AbstractTableModel { public ServiceTableModel (int r, int c) { row = r; col = c; } public int getRowCount() { return row; } public int getColumnCount() { return col; } public String getColumnName(int c) { return columnNames[c]; } public Class<?> getColumnClass(int c) { return serviceTable[0][c].getClass(); } public Object getValueAt (int r, int c) { if (c == 0) return mf.format(serviceTable[r][c]); if (c == 4) return cf.format(serviceTable[r][c]); return serviceTable [r][c]; } private int row; private int col; } // Send message dialog public void sendMessage(String msg) { JOptionPane.showMessageDialog( servicePanel, msg, "Message Dialog", JOptionPane.INFORMATION_MESSAGE); } }
Here is the run time error:
C:\Documents and Settings\pmll42\Desktop\Java\Auto History>java AutoHistory Exception occurred during event dispatching: java.lang.IllegalArgumentException: Cannot format given Object as a Number at java.text.DecimalFormat.format(Unknown Source) at java.text.Format.format(Unknown Source) at javax.swing.JTable$DoubleRenderer.setValue(Unknown Source) at javax.swing.table.DefaultTableCellRenderer.getTableCellRendererCompon ent(Unknown Source) at javax.swing.JTable.prepareRenderer(Unknown Source) at javax.swing.plaf.basic.BasicTableUI.paintCell(Unknown Source) at javax.swing.plaf.basic.BasicTableUI.paintCells(Unknown Source) at javax.swing.plaf.basic.BasicTableUI.paint(Unknown Source) at javax.swing.plaf.ComponentUI.update(Unknown Source) at javax.swing.JComponent.paintComponent(Unknown Source) at javax.swing.JComponent.paint(Unknown Source) at javax.swing.JComponent.paintChildren(Unknown Source) at javax.swing.JComponent.paint(Unknown Source) at javax.swing.JViewport.paint(Unknown Source) at javax.swing.JComponent.paintChildren(Unknown Source) at javax.swing.JComponent.paint(Unknown Source) at javax.swing.JComponent.paintChildren(Unknown Source) at javax.swing.JComponent.paint(Unknown Source) at javax.swing.JComponent.paintChildren(Unknown Source) at javax.swing.JComponent.paint(Unknown Source) at javax.swing.JLayeredPane.paint(Unknown Source) at javax.swing.JComponent.paintChildren(Unknown Source) at javax.swing.JComponent.paintToOffscreen(Unknown Source) at javax.swing.BufferStrategyPaintManager.paint(Unknown Source) at javax.swing.RepaintManager.paint(Unknown Source) at javax.swing.JComponent.paint(Unknown Source) at java.awt.GraphicsCallback$PaintCallback.run(Unknown Source) at sun.awt.SunGraphicsCallback.runOneComponent(Unknown Source) at sun.awt.SunGraphicsCallback.runComponents(Unknown Source) at java.awt.Container.paint(Unknown Source) at java.awt.Window.paint(Unknown Source) at javax.swing.RepaintManager.paintDirtyRegions(Unknown Source) at javax.swing.RepaintManager.paintDirtyRegions(Unknown Source) at javax.swing.RepaintManager.seqPaintDirtyRegions(Unknown Source) at javax.swing.SystemEventQueueUtilities$ComponentWorkRequest.run(Unknow n Source) at java.awt.event.InvocationEvent.dispatch(Unknown Source) at java.awt.EventQueue.dispatchEvent(Unknown Source) at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source) at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source) at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source) at java.awt.Dialog$1.run(Unknown Source) at java.awt.Dialog$3.run(Unknown Source) at java.security.AccessController.doPrivileged(Native Method) at java.awt.Dialog.show(Unknown Source) at java.awt.Component.show(Unknown Source) at java.awt.Component.setVisible(Unknown Source) at java.awt.Window.setVisible(Unknown Source) at java.awt.Dialog.setVisible(Unknown Source) at Service$1.run(service.java:42) at java.awt.event.InvocationEvent.dispatch(Unknown Source) at java.awt.EventQueue.dispatchEvent(Unknown Source) at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source) at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source) at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source) at java.awt.EventDispatchThread.pumpEvents(Unknown Source) at java.awt.EventDispatchThread.pumpEvents(Unknown Source) at java.awt.EventDispatchThread.run(Unknown Source)
Campbell Ritchie
Marshal
Posts: 79180
377
posted 13 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
Which line do you get the Exception at?
Mike Lipay
Ranch Hand
Posts: 171
posted 13 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
Campbell Ritchie wrote:
Which line do you get the Exception at?
#182: if (c == 4) return cf.format(serviceTable[r][c]);
Did you see how Paul
cut 87% off of his electric heat bill with 82 watts of micro heaters
?
reply
reply
Bookmark Topic
Watch Topic
New Topic
Boost this thread!
Similar Threads
Table cells not editable
Out of bound on removeRow
Is there a way to show an image in a JTable as a thumbnail?
How do I pass a frame into another class?
JTable dates
More...