Editar e apagar dados de um jtable

3 respostas Resolvido
jtable
Eduardo_Pertile

Bom dia pessoal, gostaria, se possível, um ajuda de como editar dados de um array<> em um jtable.
Preciso pegar uns dados que adicioneu em um jtable vindos de array e fazer funcionar o botão EDITAR e Excluir.

Aqui vai a codificação de mostrar os dados em um jTable:

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
  String palavra = jTextField2.getText();
  String descricao = jTextField3.getText();
  String codigo = jTextField1.getText();
  Palavra p = new Palavra(codigo, palavra, descricao);
  
  dados.cadastraPalavra(p);
  total.setText(dados.numPalavras() + ".");
  
  DefaultTableModel model =(DefaultTableModel) jTable3.getModel();
  
  model.addRow(new Object[] {
    jTextField1.getText(), jTextField2.getText(), jTextField3.getText()
  });
  
  System.out.println(p);
  
  JOptionPane.showMessageDialog(this, "Palavra cadastrada com sucesso!");
  jTextField1.setText("");
  jTextField2.setText("");
  jTextField3.setText("");

Eu precisaria de exemplo pra entender melhor, sou iniciante dos iniciantes.

3 Respostas

Eduardo_Pertile

código que implementei na exclusão é esse:

int posicao = jTable3.getSelectedRow();
palavras.remove(posicao);

e ocorre esse erro:

Exception in thread "AWT-EventQueue-0" java.lang.IndexOutOfBoundsException: Index 0 out of bounds for length 0
	at java.base/jdk.internal.util.Preconditions.outOfBounds(Preconditions.java:64)
	at java.base/jdk.internal.util.Preconditions.outOfBoundsCheckIndex(Preconditions.java:70)
	at java.base/jdk.internal.util.Preconditions.checkIndex(Preconditions.java:266)
	at java.base/java.util.Objects.checkIndex(Objects.java:359)
	at java.base/java.util.ArrayList.remove(ArrayList.java:504)
	at com.mycompany.dicionariov2_pi.telaInicial.jButton3ActionPerformed(telaInicial.java:270)
	at com.mycompany.dicionariov2_pi.telaInicial$6.actionPerformed(telaInicial.java:101)
	at java.desktop/javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:1972)
	at java.desktop/javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2313)
	at java.desktop/javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:405)
	at java.desktop/javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:262)
	at java.desktop/javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:279)
	at java.desktop/java.awt.AWTEventMulticaster.mouseReleased(AWTEventMulticaster.java:297)
	at java.desktop/java.awt.Component.processMouseEvent(Component.java:6626)
	at java.desktop/javax.swing.JComponent.processMouseEvent(JComponent.java:3389)
	at java.desktop/java.awt.Component.processEvent(Component.java:6391)
	at java.desktop/java.awt.Container.processEvent(Container.java:2266)
	at java.desktop/java.awt.Component.dispatchEventImpl(Component.java:5001)
	at java.desktop/java.awt.Container.dispatchEventImpl(Container.java:2324)
	at java.desktop/java.awt.Component.dispatchEvent(Component.java:4833)
	at java.desktop/java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4948)
	at java.desktop/java.awt.LightweightDispatcher.processMouseEvent(Container.java:4575)
	at java.desktop/java.awt.LightweightDispatcher.dispatchEvent(Container.java:4516)
	at java.desktop/java.awt.Container.dispatchEventImpl(Container.java:2310)
	at java.desktop/java.awt.Window.dispatchEventImpl(Window.java:2780)
	at java.desktop/java.awt.Component.dispatchEvent(Component.java:4833)
	at java.desktop/java.awt.EventQueue.dispatchEventImpl(EventQueue.java:773)
	at java.desktop/java.awt.EventQueue$4.run(EventQueue.java:722)
	at java.desktop/java.awt.EventQueue$4.run(EventQueue.java:716)
	at java.base/java.security.AccessController.doPrivileged(AccessController.java:399)
	at java.base/java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:86)
	at java.base/java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:97)
	at java.desktop/java.awt.EventQueue$5.run(EventQueue.java:746)
	at java.desktop/java.awt.EventQueue$5.run(EventQueue.java:744)
	at java.base/java.security.AccessController.doPrivileged(AccessController.java:399)
	at java.base/java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:86)
	at java.desktop/java.awt.EventQueue.dispatchEvent(EventQueue.java:743)
	at java.desktop/java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:203)
	at java.desktop/java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:124)
	at java.desktop/java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:113)
	at java.desktop/java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:109)
	at java.desktop/java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
	at java.desktop/java.awt.EventDispatchThread.run(EventDispatchThread.java:90)
Eduardo_Pertile

e na Edição do cadastro essa codificação aparece esse problema:

Lucas_Camara
Solucao aceita

Se vc implementar sua própria tableModel usando AbstractTableModel, vai ficar mais fácil de fazer esse tipo de coisa.

Criado 29 de março de 2022
Ultima resposta 30 de mar. de 2022
Respostas 3
Participantes 2