I'm having an error passing value formatted to JTable
, if anyone can help, thank you very much.
This error only happens when I format the number, I would like to know how I can correct it.
Code:
private void calcularButtonActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
// valor < 3.000 = 0.12
// valor >= 3.000 && valor <= 5.000 = 0.18
// valor >= 9.000 && valor <= 30.000 = 0.22
// valor >= 40.000 && valor <= 90.000 = 0.24
// valor >= 100.000 && valor <= 150.000 = 0.27
// valor >= 160.000 && valor <= 210.000 = 0.31
// valor >= 300.000 && valor <= 500.000 = 0.32
// valor >= 1.000.000 && valor <= 2.000.000 = 0.34
String posicao = posicaoTextField.getText();
String nome = nomeTextField.getText();
float valor = Float.valueOf(valorTextField.getText());
float valorVenda = 0;
DefaultTableModel dtm = (DefaultTableModel) valoresTableView.getModel();
NumberFormat nf = NumberFormat.getCurrencyInstance();
DecimalFormat df = new DecimalFormat("R$ ###,###.00");
if (valor < 3000) {
valorVenda = (float) (valor * 0.095);
float valorTotalVenda = valor + valorVenda;
System.out.println(nf.format(valorTotalVenda));
}
if (valor >= 3000 && valor <= 8000 ) {
valorVenda = (float) (valor * 0.137);
float valorTotalVenda = valor + valorVenda;
System.out.println(nf.format(valorTotalVenda));
}
if (valor >= 9000 && valor <= 39999) {
valorVenda = (float) (valor * 0.163);
float valorTotalVenda = valor + valorVenda;
System.out.println(nf.format(valorTotalVenda));
}
if (valor >= 40000 && valor <= 99999) {
valorVenda = (float) (valor * 0.185);
float valorTotalVenda = valor + valorVenda;
System.out.println(nf.format(valorTotalVenda));
}
if (valor >= 100000 && valor <= 159999) {
valorVenda = (float) (valor * 0.191);
float valorTotalVenda = valor + valorVenda;
System.out.println(nf.format(valorTotalVenda));
}
if (valor >= 160000 && valor <= 219999) {
valorVenda = (float) (valor * 0.217);
float valorTotalVenda = valor + valorVenda;
System.out.println(nf.format(valorTotalVenda));
}
if (valor >= 300000 && valor <= 999999) {
valorVenda = (float) (valor * 0.223);
float valorTotalVenda = valor + valorVenda;
float valorGanho = valorTotalVenda - valor;
dtm.addRow(new Object[]{nome,
posicao,
df.format(valor).toString(),
df.format(valorTotalVenda),
df.format(valorGanho)});
}
if (valor >= 1000000 && valor <= 9999999) {
valorVenda = (float) (valor * 0.239);
float valorTotalVenda = valor + valorVenda;
float valorGanho = valorTotalVenda - valor;
dtm.addRow(new Object[]{nome,
posicao,
df.format(valor).toString(),
df.format(valorTotalVenda),
df.format(valorGanho)});
}
}
And the error that appears is this:
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.paintToOffscreen(JComponent.java:5210) at javax.swing.BufferStrategyPaintManager.paint(BufferStrategyPaintManager.java:290) at javax.swing.RepaintManager.paint(RepaintManager.java:1272) at javax.swing.JComponent._paintImmediately(JComponent.java:5158) at javax.swing.JComponent.paintImmediately(JComponent.java:4969) at javax.swing.RepaintManager$4.run(RepaintManager.java:831) 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)