2 Replies Latest reply: Jun 25, 2013 9:24 AM by mKorbel RSS

    problem with jtable + timer

    ac98e1f7-4426-4d2a-bbc7-bfb7277ae3ef

      hello I'm new to this java and I have a big problem and I need help, and I try to update a jtable with a timer every 15 seconds, but when you upgrade comes a random point the program crashes, I problado around and can not find how to make it work, the filling method deals elsewhere in the program which works well.
      Here is the code for you to see and error.
      Excuse my English because I do not speak very well, thank you very much greetings


      Java

       

      package Pantallas;

      import Clases.orden;

      import java.awt.Color;

      import java.awt.Component;

      import java.sql.SQLException;

      import java.util.Calendar;

      import java.util.Date;

      import java.util.Timer;

      import java.util.TimerTask;

      import java.util.logging.Level;

      import java.util.logging.Logger;

      import javax.swing.JLabel;

      import javax.swing.JTable;

      import javax.swing.SwingConstants;

      import javax.swing.table.DefaultTableModel;

      import javax.swing.table.TableCellRenderer;

      import javax.swing.table.TableColumn;

       

      public class Liquidacion extends javax.swing.JFrame {

       

          Principal P = null;

          public String valorBuscado = "";

          Timer timer = new Timer(); // El timer que se encarga de administrar los tiempo de repeticion

          public int segundos; // manejar el valor del contador

          Date antes = new Date();

          Date despues = new Date();

          orden o = new orden();

          int cont = 0;

       

          public Liquidacion() {

              initComponents();

          }

       

          Liquidacion(Principal aThis) {

              initComponents();

              P = aThis;

              try {

       

                  Start(15);

       

              } catch (Exception ex) {

                  Logger.getLogger(Liquidacion.class.getName()).log(Level.SEVERE, null, ex);

              }

       

          }

                 

       

          class MiTarea extends TimerTask {

       

              public void run() {

       

                  orden o = new orden();

                  antes = new Date();

                  Calendar calendar = Calendar.getInstance(); //obtiene la fecha de hoy

                  calendar.add(Calendar.DATE, -20);

                  antes.setDate(calendar.get(Calendar.DATE));

                  antes.setMonth(calendar.get(Calendar.MONTH));

                  antes.setYear(calendar.get(Calendar.YEAR) - 1900);

                  despues = new Date();

       

                  DefaultTableModel value;

                  value = new DefaultTableModel() {

                      Class[] types = new Class[]{

                          java.lang.String.class, java.lang.String.class, java.lang.String.class, java.lang.String.class, java.lang.String.class, java.lang.String.class, java.lang.String.class, java.lang.String.class

                      };

                      boolean[] canEdit = new boolean[]{

                          false, false, false, false, false, false, false, false

                      };

       

                      @Override

                      public Class getColumnClass(int columnIndex) {

                          return types[columnIndex];

                      }

       

                      @Override

                      public boolean isCellEditable(int rowIndex, int columnIndex) {

                          return canEdit[columnIndex];

                      }

                  };

       

       

                  Object[] Columnas = {"NAVE/VIAJE", "PUERTO", "NAVE", "ARMADOR", "FECHA ARRIVO", "FECHA ZARPE", "USUARIO A COMPLETAR", "DIAS FALTANTES"};

                  for (Object Col : Columnas) {

                      value.addColumn(Col);

                  }

       

                  tablaRegistros.setModel(value);

       

                  try {

                      tablaRegistros.setModel(o.ListarTabla1("", antes, despues, value));

                                    

                  } catch (SQLException ex) {

                      System.out.println("error tabla:");

                      Logger.getLogger(Liquidacion.class.getName()).log(Level.SEVERE, null, ex);

                  }

       

              }// end run()

          }// end SincronizacionAutomatica

       

       

          public void Start(int pSeg) throws Exception {

              // le asignamos una tarea al timer

              timer.schedule(new MiTarea(), 0, pSeg * 1000);

          }// end Start

      }

       

      this is the error


       

      Exception in thread "AWT-EventQueue-0" java.lang.ArrayIndexOutOfBoundsException: 0 >= 0         =====> or this Exception in thread "AWT-EventQueue-0" java.lang.ArrayIndexOutOfBoundsException: 5>=5

          at java.util.Vector.elementAt(Vector.java:470)

          at javax.swing.table.DefaultTableColumnModel.getColumn(DefaultTableColumnModel.java:294)

          at javax.swing.plaf.basic.BasicTableHeaderUI.getHeaderHeight(BasicTableHeaderUI.java:733)

          at javax.swing.plaf.basic.BasicTableHeaderUI.createHeaderSize(BasicTableHeaderUI.java:765)

          at javax.swing.plaf.basic.BasicTableHeaderUI.getPreferredSize(BasicTableHeaderUI.java:796)

          at javax.swing.JComponent.getPreferredSize(JComponent.java:1660)

          at javax.swing.ViewportLayout.preferredLayoutSize(ViewportLayout.java:95)

          at java.awt.Container.preferredSize(Container.java:1788)

          at java.awt.Container.getPreferredSize(Container.java:1773)

          at javax.swing.JComponent.getPreferredSize(JComponent.java:1662)

          at javax.swing.ScrollPaneLayout.layoutContainer(ScrollPaneLayout.java:723)

          at java.awt.Container.layout(Container.java:1503)

          at java.awt.Container.doLayout(Container.java:1492)

          at java.awt.Container.validateTree(Container.java:1688)

          at java.awt.Container.validate(Container.java:1623)

          at javax.swing.RepaintManager$2.run(RepaintManager.java:679)

          at javax.swing.RepaintManager$2.run(RepaintManager.java:677)

          at java.security.AccessController.doPrivileged(Native Method)

          at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)

          at javax.swing.RepaintManager.validateInvalidComponents(RepaintManager.java:676)

          at javax.swing.RepaintManager$ProcessingRunnable.run(RepaintManager.java:1650)

          at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:251)

          at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:727)

          at java.awt.EventQueue.access$200(EventQueue.java:103)

          at java.awt.EventQueue$3.run(EventQueue.java:688)

          at java.awt.EventQueue$3.run(EventQueue.java:686)

          at java.security.AccessController.doPrivileged(Native Method)

          at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)

          at java.awt.EventQueue.dispatchEvent(EventQueue.java:697)

          at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:242)

          at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:161)

          at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:150)

          at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:146)

          at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:138)

          at java.awt.EventDispatchThread.run(EventDispatchThread.java:91)