Holas amigos de foros del web, les tengo esta consulta, he hecho una app CRUD de planteles(lugares) con 5 campos,todas las operaciones crud funcionan bien, mi problema es mostrarlas actualizadas en el jtable.
El programa funciona así: levanta el formulario sin ningún dato pero con el método jTableEventos(), hago click en el botón "buscar" y me muestra los planteles, agrego un nuevo registro y hago clicj en "buscar" y me devuelve todos los planteles más el registro insertado, hasta allí todo bien.
Luego selecciono un registro y se muestra en jtextfields los 5 campos, los edito, grabo (UPDATE) , lo efectúa correctamente, PERO hago click en buscar para que me muestre los registros, con el último registro modificado y sale este error:
java.lang.ArrayIndexOutOfBoundsException: -1
at java.util.Vector.elementAt(Vector.java:430)
at javax.swing.table.DefaultTableModel.getValueAt(Def aultTableModel.java:632)
at javax.swing.JTable.getValueAt(JTable.java:2695)
at vista.PlantelFrame.jTable1_SelectedIndexChanged(Pl antelFrame.java:305)
at vista.PlantelFrame.access$700(PlantelFrame.java:21 )
at vista.PlantelFrame$8.valueChanged(PlantelFrame.jav a:319)
at javax.swing.DefaultListSelectionModel.fireValueCha nged(DefaultListSelectionModel.java:167)
at javax.swing.DefaultListSelectionModel.fireValueCha nged(DefaultListSelectionModel.java:147)
at javax.swing.DefaultListSelectionModel.fireValueCha nged(DefaultListSelectionModel.java:194)
at javax.swing.DefaultListSelectionModel.changeSelect ion(DefaultListSelectionModel.java:388)
at javax.swing.DefaultListSelectionModel.changeSelect ion(DefaultListSelectionModel.java:398)
at
javax.swing.DefaultListSelectionModel.removeSelect ionIntervalImpl(DefaultListSelectionModel.java:559 )
at javax.swing.DefaultListSelectionModel.clearSelecti on(DefaultListSelectionModel.java:403)
at javax.swing.JTable.clearSelection(JTable.java:2079 )
at javax.swing.JTable.clearSelectionAndLeadAnchor(JTa ble.java:2087)
at javax.swing.JTable.tableChanged(JTable.java:4367)
at javax.swing.JTable.setModel(JTable.java:3685)
at vista.PlantelFrame.BtnBuscarActionPerformed(Plante lFrame.java:265)
at vista.PlantelFrame.access$600(PlantelFrame.java:21 )
at vista.PlantelFrame$7.actionPerformed(PlantelFrame. java:176)
at javax.swing.AbstractButton.fireActionPerformed(Abs tractButton.java:1995)
at javax.swing.AbstractButton$Handler.actionPerformed (AbstractButton.java:2318)
at javax.swing.DefaultButtonModel.fireActionPerformed (DefaultButtonModel.java:387)
at javax.swing.DefaultButtonModel.setPressed(DefaultB uttonModel.java:242)
at javax.swing.plaf.basic.BasicButtonListener.mouseRe leased(BasicButtonListener.java:236)
at java.awt.Component.processMouseEvent(Component.jav a:6134)
at javax.swing.JComponent.processMouseEvent(JComponen t.java:3265)
at java.awt.Component.processEvent(Component.java:589 9)
at java.awt.Container.processEvent(Container.java:202 3)
at java.awt.Component.dispatchEventImpl(Component.jav a:4501)
at java.awt.Container.dispatchEventImpl(Container.jav a:2081)
at java.awt.Component.dispatchEvent(Component.java:43 31)
at java.awt.LightweightDispatcher.retargetMouseEvent( Container.java:4301)
at java.awt.LightweightDispatcher.processMouseEvent(C ontainer.java:3965)
at java.awt.LightweightDispatcher.dispatchEvent(Conta iner.java:3895)
at java.awt.Container.dispatchEventImpl(Container.jav a:2067)
at java.awt.Window.dispatchEventImpl(Window.java:2458 )
at java.awt.Component.dispatchEvent(Component.java:43 31)
at java.awt.EventQueue.dispatchEvent(EventQueue.java: 599)
at java.awt.EventDispatchThread.pumpOneEventForFilter s(EventDispatchThread.java:269)
at java.awt.EventDispatchThread.pumpEventsForFilter(E ventDispatchThread.java:184)
at java.awt.EventDispatchThread.pumpEventsForHierarch y(EventDispatchThread.java:174)
at java.awt.EventDispatchThread.pumpEvents(EventDispa tchThread.java:169)
at java.awt.EventDispatchThread.pumpEvents(EventDispa tchThread.java:161)
at java.awt.EventDispatchThread.run(EventDispatchThre ad.java:122)
El mismo error sale si hago un DELETE,les pido su ayuda por favor, AQUÍ EL CÓDIGO FUENTE
================================================== =========
public PlantelFrame() {
try {
initComponents();
........
jTableEventos();
} catch (Exception e) { e.printStackTrace(); }
}
================================================== =========
private void jTableEventos(){
ListSelectionModel rowSM = jTable1.getSelectionModel();
rowSM.addListSelectionListener(new ListSelectionListener() {
public void valueChanged(ListSelectionEvent e) {
jTable1_SelectedIndexChanged(e);
}
});
}
================================================== =========
private void jTable1_SelectedIndexChanged(ListSelectionEvent e){
ListSelectionModel lsm = (ListSelectionModel)e.getSource();
int pos = lsm.getMinSelectionIndex();
this.txthc.setText(jTable1.getValueAt(pos, 0).toString());
this.txtNombrePlantel.setText(jTable1.getValueAt(p os, 1).toString());
this.txtUbicacion.setText(jTable1.getValueAt(pos, 2).toString());
this.txtNroMax.setText(jTable1.getValueAt(pos, 3).toString());
this.txtDesc.setText(jTable1.getValueAt(pos, 4).toString());
btnModificar.setEnabled(true);
btnEliminar.setEnabled(true); }
================================================== =========
private void btnGrabarActionPerformed(java.awt.event.ActionEven t evt) {
try {
if(estado==false) { ////////// CODIGO PARA HACER EL INSERT }
else{ ////////// CODIGO PARA HACER EL UPDATE }
} catch (Exception e) { e.printStackTrace(); }
}
================================================== =========
private void BtnBuscarActionPerformed(java.awt.event.ActionEven t evt) {
try { tableModelPlantel=PlantelLogic.obtenerModeloAllPla ntel();
jTable1.setModel(tableModelPlantel); }
catch (Exception e) { e.printStackTrace(); }
}
================================================== =========
private void btnEliminarActionPerformed(java.awt.event.ActionEv ent evt) {
try { ////////// CODIGO PARA HACER EL DELETE }
catch (Exception e) { e.printStackTrace(); }
}
================================================== =========