Ver Mensaje Individual
  #5 (permalink)  
Antiguo 29/05/2015, 05:27
lucho248
 
Fecha de Ingreso: octubre-2010
Mensajes: 154
Antigüedad: 13 años, 6 meses
Puntos: 5
Respuesta: actualizar un jtable en un tiempo "x"

Hola Profesor.
En realidad esta todo dentro de mi clase JFRAME que tiene la tabla.
y ese this.correr() hace referencia al jframe y llama al metodo correr que tambien claro esta dentro del frame.

paso codigo completo de la clase.

Código Clase:
Ver original
  1. package ;
  2.  
  3. import java.awt.EventQueue;
  4. import java.awt.Font;
  5. import java.awt.event.ActionEvent;
  6. import java.awt.event.ActionListener;
  7. import java.awt.event.KeyEvent;
  8. import java.awt.event.KeyListener;
  9. import java.sql.SQLException;
  10. import java.util.List;
  11. import javax.swing.JButton;
  12. import javax.swing.JFrame;
  13. import javax.swing.JLabel;
  14. import javax.swing.JOptionPane;
  15. import javax.swing.JPanel;
  16. import javax.swing.JScrollPane;
  17. import javax.swing.JTable;
  18. import javax.swing.JTextField;
  19. import javax.swing.RowFilter;
  20. import javax.swing.SwingConstants;
  21. import javax.swing.SwingUtilities;
  22. import javax.swing.Timer;
  23. import javax.swing.border.EmptyBorder;
  24. import javax.swing.event.ListSelectionEvent;
  25. import javax.swing.event.ListSelectionListener;
  26. import javax.swing.table.DefaultTableCellRenderer;
  27. import javax.swing.table.DefaultTableModel;
  28. import javax.swing.table.TableModel;
  29. import javax.swing.table.TableRowSorter;
  30.  
  31.  
  32.  
  33. public class BuscaTicket extends JFrame {
  34.     private Operador op;
  35.     private JPanel contentPane;
  36.     private JTable jtTabla;
  37.     private JTextField tfTicket;
  38.     private JScrollPane scTicket;
  39.     public DefaultTableModel model = new DefaultTableModel();  
  40.     private TableRowSorter<TableModel> modeloOrdenado;
  41.  
  42.     private DefaultTableCellRenderer alinearCentro, alinearDerecha, alinearIzquierda;
  43.     private JTextField tfFiltro;
  44.     private ListadoTicket lt;
  45.    
  46.     public int idTicket;
  47.    
  48.     /***************************/
  49.     private Timer timer;
  50.     /*
  51.      * 1000ms ---- 1s
  52.      *    xms ---- 60sx2m/s
  53.      *    xms = 120 x 1000 = 120.000ms
  54.      * */
  55.     private int delay = 120000; // every 1 second = 1000 milisegundos
  56.     /***************************/    
  57.    
  58.     public static void main(String[] args) {
  59.         EventQueue.invokeLater(new Runnable() {
  60.             public void run() {
  61.                 try {
  62.                     BuscaTicket frame = new BuscaTicket(new ListadoTicket(-1));
  63.  
  64.                 } catch (Exception e) {
  65.                     e.printStackTrace();
  66.                 }
  67.             }
  68.         });
  69.     }
  70.  
  71.  
  72.     public BuscaTicket(ListadoTicket lt) throws SQLException {
  73.         setAlwaysOnTop(true);
  74.         setResizable(false);
  75.         this.lt = lt;
  76.        
  77.         idTicket = -1;     
  78.        
  79.         setTitle("Ticket");
  80.         setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  81.         setBounds(100, 100, 325, 320);
  82.         contentPane = new JPanel();
  83.         contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
  84.         setContentPane(contentPane);
  85.         contentPane.setLayout(null);
  86.        
  87.         JButton btnCancelar = new JButton("Cancelar");
  88.         btnCancelar.setFont(new Font("Tahoma", Font.PLAIN, 11));
  89.         btnCancelar.addActionListener(new ActionListener() {
  90.             public void actionPerformed(ActionEvent arg0) {
  91.                 cancelar();
  92.             }
  93.         });
  94.         btnCancelar.setBounds(230, 258, 83, 23);
  95.         contentPane.add(btnCancelar);
  96.        
  97.         JButton btnAceptar = new JButton("Aceptar");
  98.         btnAceptar.setFont(new Font("Tahoma", Font.PLAIN, 11));
  99.         btnAceptar.addActionListener(new ActionListener() {
  100.             public void actionPerformed(ActionEvent arg0) {
  101.                 botonAceptar();
  102.             }
  103.         });
  104.         btnAceptar.setBounds(146, 258, 83, 23);
  105.         contentPane.add(btnAceptar);
  106.        
  107.         scTicket = new JScrollPane();
  108.         scTicket.setBounds(10, 39, 300, 200);
  109.         contentPane.add(scTicket);
  110.        
  111.         jtTabla = new JTable();
  112.         jtTabla.setFont(new Font("Tahoma", Font.PLAIN, 12));
  113.         scTicket.setViewportView(jtTabla);
  114.        
  115.         JLabel lblIDTicket = new JLabel("Nro. Ticket");
  116.         lblIDTicket.setBounds(10, 262, 83, 14);
  117.         contentPane.add(lblIDTicket);
  118.        
  119.         tfTicket = new JTextField();
  120.         tfTicket.setEditable(false);
  121.         tfTicket.setBounds(80, 259, 56, 20);
  122.         contentPane.add(tfTicket);
  123.         tfTicket.setColumns(10);
  124.        
  125.         JLabel lblFiltro = new JLabel("Filtro");
  126.         lblFiltro.setBounds(10, 14, 56, 14);
  127.         contentPane.add(lblFiltro);
  128.        
  129.         tfFiltro = new JTextField();
  130.         tfFiltro.setBounds(67, 11, 110, 20);
  131.         contentPane.add(tfFiltro);
  132.         tfFiltro.setColumns(10);
  133.        
  134.         JButton btnActualizar = new JButton("Actualizar");
  135.         btnActualizar.setFont(new Font("Tahoma", Font.PLAIN, 11));
  136.         btnActualizar.addActionListener(new ActionListener() {
  137.             public void actionPerformed(ActionEvent arg0) {
  138.                 initTabla();
  139.             }
  140.         });
  141.         btnActualizar.setBounds(200, 10, 83, 23);
  142.         contentPane.add(btnActualizar);
  143.        
  144.         this.initAlineacionTablas();
  145.         this.initTabla();
  146.        
  147.         this.setVisible(true);
  148.         this.autoFiltro(); 
  149.        
  150.         this.correr();
  151.        
  152.     }
  153.    
  154.     public void cerrar(){
  155.         this.detenerTIMER();
  156.         this.dispose();
  157.     }
  158.  
  159.     public void botonAceptar(){            
  160.         try {
  161.             if (idTicket > 0 )
  162.             {          
  163.                 this.lt.cargaTicket(idTicket, this);
  164.                 this.setVisible(false);
  165.             }
  166.         } catch (Exception e) {
  167.             JOptionPane.showMessageDialog(null, "Datos mal ingresados");
  168.         }      
  169.     }
  170.    
  171.     public void cancelar(){
  172.         this.detenerTIMER();
  173.         this.dispose();
  174.     }
  175.    
  176.    
  177.     /**CARGA TABLAS LISTA DE TICKETS */
  178.     public void initTabla(){
  179.         model = new DefaultTableModel(
  180.             new Object[][] {
  181.                     {null, null, null, null},
  182.                     {null, null, null, null},
  183.                     {null, null, null, null},
  184.                     {null, null, null, null},
  185.                     {null, null, null, null},
  186.                     {null, null, null, null},
  187.                     {null, null, null, null},
  188.                     {null, null, null, null},
  189.                     {null, null, null, null},
  190.                     {null, null, null, null},
  191.                     {null, null, null, null},
  192.             },
  193.             new String[] {
  194.                 "ID", "Ticket", "Fecha", "Art. Distintos"
  195.             }
  196.         ) {
  197.             Class[] columnTypes = new Class[] {
  198.                 String.class, String.class, String.class, String.class
  199.             };
  200.             public Class getColumnClass(int columnIndex) {
  201.                 return columnTypes[columnIndex];
  202.             }
  203.         };
  204.         jtTabla.setModel(model);
  205.         jtTabla.getColumnModel().getColumn(0).setPreferredWidth(40);
  206.         jtTabla.getSelectionModel().addListSelectionListener(new RowListener());
  207.              
  208.         List<Venta>list = null;
  209.         list = FisicaVenta.listVenta();
  210.        
  211.         if(list.size() != 0){
  212.             model.setRowCount(0);
  213.             int art;
  214.             String fecha;
  215.             for (Venta x : list) {
  216.                 art = x.size();
  217.                 fecha = Util.getDate(x.getFecha());
  218.                 model.addRow(new Object[] {
  219.                         x.getId(),
  220.                         x.getNumeroVenta(),
  221.                         fecha,
  222.                         art
  223.                         });
  224.             }
  225.         }
  226.        
  227.         jtTabla.getColumnModel().getColumn(0).setCellRenderer(alinearCentro);
  228.         jtTabla.getColumnModel().getColumn(1).setCellRenderer(alinearCentro);
  229.         jtTabla.getColumnModel().getColumn(2).setCellRenderer(alinearCentro);
  230.         jtTabla.getColumnModel().getColumn(3).setCellRenderer(alinearCentro);
  231.        
  232.         jtTabla.setToolTipText("Lista de ticket"); 
  233.     }
  234.    
  235.     /*Inicializo y cargo alineacinn de conlumnas en tablas*/
  236.     public void initAlineacionTablas(){
  237.         alinearCentro = new DefaultTableCellRenderer();
  238.         alinearDerecha = new DefaultTableCellRenderer();
  239.         alinearIzquierda = new DefaultTableCellRenderer();
  240.    
  241.         alinearCentro.setHorizontalAlignment(SwingConstants.CENTER);
  242.         alinearDerecha.setHorizontalAlignment(SwingConstants.RIGHT);
  243.         alinearIzquierda.setHorizontalAlignment(SwingConstants.LEFT);
  244.     }
  245.    
  246.    
  247.       //METODOS PARA OBTENER LAS FILAS SELECIONADAS
  248.     private int outputSelection() {
  249.         int row = jtTabla.getSelectedRow();
  250.         return row;
  251.     }
  252.    
  253.     private class RowListener implements ListSelectionListener {
  254.         public void valueChanged(ListSelectionEvent event) {
  255.            try {
  256.                if (event.getValueIsAdjusting()) {
  257.                    return;
  258.                }
  259.                int x = outputSelection();
  260.                String padron, nroTicket;
  261.                int nro;
  262.                idTicket = (Integer) jtTabla.getValueAt(x, 0);
  263.                tfTicket.setText(Integer.toString(idTicket));              
  264.         } catch (Exception e) {
  265.                 System.out.println("error de filtro y selección");
  266.             }          
  267.         }      
  268.     }
  269.  
  270.    
  271.    
  272.     private void filtrar(String filtrar) {
  273.         //codigo de filtro
  274.         modeloOrdenado = new TableRowSorter(model);
  275.         tfFiltro.setText(filtrar);
  276.         String fil = filtrar;
  277.         if (fil.length() != 0){
  278.                 modeloOrdenado.setRowFilter (RowFilter.regexFilter(".*"+fil+"*."));
  279.         }else{
  280.                 modeloOrdenado.setRowFilter (RowFilter.regexFilter(""));                
  281.         }
  282.         jtTabla.setRowSorter (modeloOrdenado);
  283.        
  284.     }
  285.    
  286.    
  287.     /**********Evento temporal para actualizar la tabla*****************/
  288.     public void correr(){      
  289.         SwingUtilities.invokeLater(new Runnable()
  290.         {            
  291.             public void run()
  292.             {
  293.                 ActionListener action = new ActionListener()
  294.                 {  
  295.                     public void actionPerformed(ActionEvent event)
  296.                     {
  297.                         initTabla();
  298.                         System.out.println("inicializo tabla");
  299.                     }
  300.                 };
  301.                 timer = new Timer(delay, action);
  302.                 timer.setInitialDelay(0);
  303.                 timer.start();
  304.             }
  305.         });
  306.     }    
  307.    
  308.     public void detenerTIMER(){
  309.          timer.stop();
  310.     }
  311. }

No se si esta bien aplicado pero por lo menos me dio resultado
Abrazo.