0
I’m testing an event on a JTable
of the kind tableMouseClicked(evt)
, only that I am having some errors when returning the values in the respective Jtextfield and in a Jspinner that I configured. Below the Stack Trace for the output given when I click on a line to pass the amount of the type Integer
within a Jspinner:
Exception in thread "AWT-EventQueue-0" java.lang.IllegalArgumentException: illegal value at javax.swing.SpinnerNumberModel.setValue(SpinnerNumberModel.java:443) at javax.swing.JSpinner.setValue(JSpinner.java:354) at com.exemplo.view.ViewPedidos.tableMouseClicked(ViewPedidos.java:410) at com.exemplo.view.ViewPedidos.access$100(ViewPedidos.java:22) at com.exemplo.view.ViewPedidos$2.mouseClicked(ViewPedidos.java:210) at java.awt.AWTEventMulticaster.mouseClicked(AWTEventMulticaster.java:270) at java.awt.Component.processMouseEvent(Component.java:6536) at javax.swing.JComponent.processMouseEvent(JComponent.java:3324) at java.awt.Component.processEvent(Component.java:6298) at java.awt.Container.processEvent(Container.java:2236) at java.awt.Component.dispatchEventImpl(Component.java:4889) at java.awt.Container.dispatchEventImpl(Container.java:2294) at java.awt.Component.dispatchEvent(Component.java:4711) at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4888) at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4534) at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4466) at java.awt.Container.dispatchEventImpl(Container.java:2280) at java.awt.Window.dispatchEventImpl(Window.java:2746) at java.awt.Component.dispatchEvent(Component.java:4711) at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:758) 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.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:86) at java.awt.EventQueue$4.run(EventQueue.java:731) at java.awt.EventQueue$4.run(EventQueue.java:729) at java.security.AccessController.doPrivileged(Native Method) at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:76) at java.awt.EventQueue.dispatchEvent(EventQueue.java:728) 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)
This error is also happening but this is when some column of the table is null
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException at com.exemplo.view.ViewPedidos.tableMouseClicked(ViewPedidos.java:382) at com.exemplo.view.ViewPedidos.access$100(ViewPedidos.java:22) at com.exemplo.view.ViewPedidos$2.mouseClicked(ViewPedidos.java:210) at java.awt.AWTEventMulticaster.mouseClicked(AWTEventMulticaster.java:270) at java.awt.Component.processMouseEvent(Component.java:6536) at javax.swing.JComponent.processMouseEvent(JComponent.java:3324) at java.awt.Component.processEvent(Component.java:6298) at java.awt.Container.processEvent(Container.java:2236) at java.awt.Component.dispatchEventImpl(Component.java:4889) at java.awt.Container.dispatchEventImpl(Container.java:2294) at java.awt.Component.dispatchEvent(Component.java:4711) at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4888) at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4534) at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4466) at java.awt.Container.dispatchEventImpl(Container.java:2280) at java.awt.Window.dispatchEventImpl(Window.java:2746) at java.awt.Component.dispatchEvent(Component.java:4711) at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:758) 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.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:86) at java.awt.EventQueue$4.run(EventQueue.java:731) at java.awt.EventQueue$4.run(EventQueue.java:729) at java.security.AccessController.doPrivileged(Native Method) at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:76) at java.awt.EventQueue.dispatchEvent(EventQueue.java:728) 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)
And here the implementation that I’m trying to run, it’s just taking the fields of ID, Data Registration, Product Name and Control Number and retrieves only these values, while the Jtextfields Unit Value and Client Code cannot recover even if they are inserted in the bank:
private void tableMouseClicked(java.awt.event.MouseEvent evt) {
int index = table.getSelectedRow();
PedidoModel pm = (PedidoModel) table.getModel();
String id = pm.getValueAt(index, 0).toString();
String dataCadastro = pm.getValueAt(index, 1).toString();
String nomeProduto = pm.getValueAt(index, 2).toString();
String numControle = pm.getValueAt(index, 3).toString();
String quantidade = pm.getValueAt(index, 4).toString();
String valorUnitario = pm.getValueAt(index, 6).toString();
String codCliente = pm.getValueAt(index, 7).toString();
if(pm.getValueAt(index, 0) == null ){
tfPedido.setText("");
} else
if(pm.getValueAt(index, 1) == null){
tfDataCad.setText("");
} else
if(pm.getValueAt(index, 2) == null){
tfNomeProduto.setText("");
} else
if(pm.getValueAt(index, 3) == null){
tfNumControle.setText("");
} else
if(pm.getValueAt(index, 4) == null){
spQuantidade.setValue(0);
} else
if(pm.getValueAt(index, 6) == null){
tfValor.setText("");
} else
if(pm.getValueAt(index, 7) == null){
tfCodCliente.setText("");
} else {
tfPedido.setText(id);
tfDataCad.setText(dataCadastro);
tfNomeProduto.setText(nomeProduto);
tfNumControle.setText(numControle);
spQuantidade.setValue(quantidade);
tfValor.setText(valorUnitario);
tfCodCliente.setText(codCliente);
}
}
And down with the comic:
+----+---------------------+-----------------------------------+-------------+------------+------------+---------------+------------+
| id | dataCadastro | nomeProduto | numControle | quantidade | valorTotal | valorUnitario | codCliente |
+----+---------------------+-----------------------------------+-------------+------------+------------+---------------+------------+
| 1 | 2017-04-05 00:00:00 | Lavadora 5Kg Brastemp | 14221 | 3 | 0 | 942.44 | NULL |
| 2 | 2017-04-05 00:00:00 | Computador Positivo Intel Core I5 | 41141 | 1 | 0 | 2044.44 | NULL |
| 4 | 2017-04-05 00:00:00 | Produto X | 434342 | 7 | 0 | 444.55 | 3443432 |
| 6 | 2017-04-05 00:00:00 | Mesa Tampo de Granito Bartira | 1333131 | 6 | 0 | 4444.44 | 44333 |
| 7 | 2017-04-05 00:00:00 | Produto X | 434342 | 7 | 0 | 444.55 | 3443432 |
| 8 | 2017-04-05 00:00:00 | Produto X | 434342 | 7 | 0 | 444.55 | 3443432 |
| 14 | 2017-04-06 00:00:00 | Produto A | 3244333 | 4 | 0 | 555.55 | 34334242 |
| 15 | 2017-04-06 00:00:00 | Produto C | 434343 | 2 | 0 | 444.44 | 4434334 |
+----+---------------------+-----------------------------------+-------------+------------+------------+---------------+------------+
Below follows the model Pedidomodel:
package com.exemplo.table;
import com.exemplo.conexao.ConnectionFactory;
import com.exemplo.controller.PedidoDAO;
import com.exemplo.exception.DAOException;
import com.exemplo.model.Pedido;
import java.util.ArrayList;
import java.util.List;
import javax.swing.table.AbstractTableModel;
/**
*
* @author Vickz
*/
public class PedidoModel extends AbstractTableModel {
private PedidoDAO pedidos;
private List<Pedido> listpedidos = new ArrayList<>();
public PedidoModel(PedidoDAO pedidos) throws DAOException {
this.pedidos = pedidos;
}
public void atualizarModel() throws DAOException {
listpedidos = pedidos.listar();
}
@Override
public String getColumnName(int column) {
switch (column) {
case 0:
return "ID";
case 1:
return "Data de Cadastro";
case 2:
return "Nome do Produto";
case 3:
return "Num Controle";
case 4:
return "Quantidade";
case 5:
return "Valor Unitário";
case 6:
return "Cód Cliente";
default:
return "[no]";
}
}
@Override
public int getRowCount() {
return listpedidos.size();
}
@Override
public int getColumnCount() {
return 7;
}
@Override
public Object getValueAt(int line, int column) {
Pedido pedido = listpedidos.get(line);
switch (column) {
case 0:
return pedido.getId();
case 1:
return pedido.getDataCadastro();
case 2:
return pedido.getNomeProduto();
case 3:
return pedido.getNumControle();
case 4:
return pedido.getQuantidade();
case 5:
return pedido.getValorUnitario();
case 6:
return pedido.getCodCliente();
default:
return "";
}
}
I tried to insert each field by making a condition if the seat column is null hence the JTextField
receive empty text field or the JSpinner
set zero and still errors persist. How to fix this error?
Where exactly are the lines 410 and 382 of the class
ViewPedidos.java
?– user28595
Line 382 = String valueUnite = pm.getValueAt(index, 6). toString(); Line 410 = spQuantidade.setValue(quantity);
– Victor
spquantidade is a jspinner?
– user28595
That’s right! dai I’m trying to recover the value of the quantity column in the table and insert this value in Jspinner
– Victor
You’re passing
quantidade
as String being that your Spinner expects aInteger
. Parse before doing setvalue.– user28595
As for the other problem of fields that are not recoverable, you need to see your Tablemodel.
– user28595
Thank you so much for your help!! I was able to recover the value of the column int field in Jspinner now and it worked!! Tablemodel just sent him up here
– Victor