Saludoss, tengo un problemilla al mostrar el contenido de una base de datos en una tabla JTable, en un proyecto .java usando netbeans.
He intentado observar la salida de la columna con la que me da error, se trata de la 5ª columna un dato de tipo Float, la 4ª posición del String[], y los datos que me devuelve son correctos....
Con lo que no consigo saber porque me aborta con este error:
Exception in thread "AWT-EventQueue-0" java.lang.IllegalArgumentException: Cannot format given Object as a Number
at java.text.DecimalFormat.format(DecimalFormat.java:507)
at java.text.Format.format(Format.java:157)
at javax.swing.JTable$DoubleRenderer.setValue(JTable.java:5356)
at javax.swing.table.DefaultTableCellRenderer.getTableCellRendererComponent(DefaultTableCellRenderer.java:257)
at javax.swing.JTable.prepareRenderer(JTable.java:5723)
at javax.swing.plaf.basic.BasicTableUI.paintCell(BasicTableUI.java:2114)
at javax.swing.plaf.basic.BasicTableUI.paintCells(BasicTableUI.java:2016)
at javax.swing.plaf.basic.BasicTableUI.paint(BasicTableUI.java:1812)
at javax.swing.plaf.ComponentUI.update(ComponentUI.java:161)
at javax.swing.JComponent.paintComponent(JComponent.java:780)
at javax.swing.JComponent.paint(JComponent.java:1056)
at javax.swing.JComponent.paintChildren(JComponent.java:889)
at javax.swing.JComponent.paint(JComponent.java:1065)
at javax.swing.JViewport.paint(JViewport.java:728)
at javax.swing.JComponent.paintChildren(JComponent.java:889)
at javax.swing.JComponent.paint(JComponent.java:1065)
at javax.swing.JComponent.paintChildren(JComponent.java:889)
at javax.swing.JComponent.paint(JComponent.java:1065)
at javax.swing.JComponent.paintChildren(JComponent.java:889)
at javax.swing.JComponent.paint(JComponent.java:1065)
at javax.swing.JComponent.paintChildren(JComponent.java:889)
at javax.swing.JComponent.paint(JComponent.java:1065)
at javax.swing.JLayeredPane.paint(JLayeredPane.java:586)
at javax.swing.JComponent.paintChildren(JComponent.java:889)
at javax.swing.JComponent.paintToOffscreen(JComponent.java:5217)
at javax.swing.RepaintManager$PaintManager.paintDoubleBuffered(RepaintManager.java:1579)
at javax.swing.RepaintManager$PaintManager.paint(RepaintManager.java:1502)
at javax.swing.RepaintManager.paint(RepaintManager.java:1272)
at javax.swing.JComponent.paint(JComponent.java:1042)
at java.awt.GraphicsCallback$PaintCallback.run(GraphicsCallback.java:39)
at sun.awt.SunGraphicsCallback.runOneComponent(SunGraphicsCallback.java:79)
at sun.awt.SunGraphicsCallback.runComponents(SunGraphicsCallback.java:116)
at java.awt.Container.paint(Container.java:1975)
at java.awt.Window.paint(Window.java:3904)
at javax.swing.RepaintManager$4.run(RepaintManager.java:842)
at javax.swing.RepaintManager$4.run(RepaintManager.java:814)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:76)
at javax.swing.RepaintManager.paintDirtyRegions(RepaintManager.java:814)
at javax.swing.RepaintManager.paintDirtyRegions(RepaintManager.java:789)
at javax.swing.RepaintManager.prePaintDirtyRegions(RepaintManager.java:738)
at javax.swing.RepaintManager.access$1200(RepaintManager.java:64)
at javax.swing.RepaintManager$ProcessingRunnable.run(RepaintManager.java:1732)
at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:311)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:756)
at java.awt.EventQueue.access$500(EventQueue.java:97)
at java.awt.EventQueue$3.run(EventQueue.java:709)
at java.awt.EventQueue$3.run(EventQueue.java:703)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:726)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:201)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:116)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:105)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:93)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:82)
Este es el código que estoy utilizando, en concreto un método que lo llamo en el constructor de la ventana, que me rellena el Listado con los datos de la database..
private void rellenaListado() {
//Pido a la base de datos los datos de la tabla articulo
DefaultTableModel dtm = devuelveDTM();
//Pido a la base da datos los datos de la tabla articulo
String[] datos = new String[dtm.getColumnCount()];
//Crear un objeto DataBase
DataBase db = new DataBase();
//Crear un objeto ResultSet y lo inicializamos a null
ResultSet rs = null;
try {
//Abrimos la conexión
db.abrirConexion();
//Creamos la consulta
String consulta = "SELECT * FROM articulo;";
//Ejecutamos la consulta, para eso necesitamos un objeto ResultSet
rs = db.ejecutaConsulta(consulta);
while (rs.next()) {
//Recorro el ResultSet
datos[0] = rs.getString(1);
datos[1] = rs.getString(2);
datos[2] = Integer.toString(rs.getInt(3));
datos[3] = Integer.toString(rs.getInt(4));
datos[4] = String.format("%5.2f", rs.getFloat(5));
dtm.addRow(datos);
}
} catch (MyException | SQLException ex) {
VentanaNotificaciones.ventanaError(ex.getMessage(), padre);
} finally {
try {
if (rs != null) {
//Cierro el ResultSet
db.cierraResult(rs);
}
//Cierro la conexión con la base de datos
db.cerrarConexion();
} catch (MyException ex) {
VentanaNotificaciones.ventanaError(ex.getMessage(), padre);
}
}
}
He comprobado con estos chivatos las salidas pero me sale el contenido de la tabla correcto...
//Comienzo de pruebas (chivatos)
System.out.println("Pos 0 Array:" + datos[0]);
System.out.println("Pos 1 Array:" + datos[1]);
System.out.println("Pos 2 Array:" + datos[2]);
System.out.println("Pos 3 Array:" + datos[3]);
System.out.println("String.format:" + String.format("%5.2f", rs.getFloat(5)));
System.out.println("rs.getFloat(5):" + rs.getFloat(5));
String s = String.format("%5.2f", rs.getFloat(5));
String replace = s.replace(" ","");
datos[4] = String.format("%5.2f", replace);
System.out.println("Pos 4 Array:" + datos[4]);
La columna que presenta problemas es la siguiente, si comento la línea no aborta pero, y obviamente no muestra el precio de tipo float:
datos[4] = String.format("%5.2f", rs.getFloat("5"));
Gracias a todos de ante mano, y a ver si me podeis echar un cable.
datos[4] = String.format("%5.2f", rs.getFloat(5));
?