Programación General > Java

 Refrescar JTABLE!!

(1/1)

DarkGhetto22:
Lo que quiero es refrescar el defaulttablemodel que tengo en mi clase, para cuando elimine o entre un dato nuevo el JTable se actualice, el problema es que lo he intentado con varias metodos como:

fireTableDataChanged();
.fireTableStructureChanged();
.updateUI();
.revalidate();

pero ninguno me han funcionado hasta ahora :S no se porque....bueno aca les dejo el codigo, si alguien se le ocurre algo le agradeceria mucho si me lo dejan saber


--- Código: Java(TM) 2 Platform Standard Edition 5.0 ---import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import java.sql.ResultSet;import java.sql.SQLException;import java.util.ArrayList;import java.util.List;import javax.swing.JFrame;import javax.swing.JMenuBar;import javax.swing.JMenu;import javax.swing.JMenuItem;import javax.swing.JOptionPane;import javax.swing.JTable;import javax.swing.JScrollPane;import javax.swing.table.DefaultTableModel;import javax.swing.event.TableModelEvent;import javax.swing.event.TableModelListener; public class teoriasDatos extends JFrame implements ActionListener, TableModelListener{     private final String[] titulos = {"Teoria", "Autor", "Fecha", "Ciencia", "Id"};        private JMenuBar barra;    private JMenu archivo, edicion;    private JMenuItem salir, buscar, modificar, eliminar, seleccionar;        private DefaultTableModel dtm = new DefaultTableModel();        private JTable tabla = new JTable(dtm);        private JScrollPane scroll = new JScrollPane(tabla);        private List<Integer> lista = new ArrayList<Integer>();     conexion cn = new conexion();        public teoriasDatos(){        super("Teorias System");        this.setLayout(null);        this.setSize(900, 460);        this.setResizable(false);        this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);        this.Objetos();        this.setVisible(true);    }                public void Objetos(){        barra = new JMenuBar();        archivo = new JMenu("Archivo");        edicion = new JMenu("Edicion");        buscar = new JMenuItem("Buscar");        modificar = new JMenuItem("Modificar");        eliminar = new JMenuItem("Eliminar");        seleccionar = new JMenuItem("Seleccionar");        salir = new JMenuItem("Salir");        barra.add(archivo);        barra.add(edicion);        archivo.add(salir);        edicion.add(buscar);        edicion.add(modificar);        edicion.add(eliminar);        edicion.addSeparator();        edicion.add(seleccionar);        this.setJMenuBar(barra);                          dtm.setColumnIdentifiers(titulos);        lista.clear();                try{             ResultSet aux = cn.getSt().executeQuery("SELECT*FROM datos");             while(aux.next()){                              Object [] fila = {aux.getObject(1), aux.getObject(2), aux.getObject(3),                      aux.getObject(4), aux.getObject(5)};                                  dtm.addRow(fila);                                  lista.add((Integer)aux.getObject(5));             }                     }catch(SQLException ioe){      JOptionPane.showMessageDialog(null, "Error al leer teorias: " + ioe);        }                 scroll.setBounds(0, 0, 900, 460);        this.add(scroll);                salir.addActionListener(this);        buscar.addActionListener(this);        modificar.addActionListener(this);        eliminar.addActionListener(this);        seleccionar.addActionListener(this);        dtm.addTableModelListener(tabla);                           }    public void actionPerformed(ActionEvent e) {        if(e.getSource()==buscar){            try{                int i = Integer.parseInt(JOptionPane.showInputDialog("ID de la teoria a buscar"));                ResultSet resultado = cn.buscar(i);                tabla.changeSelection(i-1, i, false, false);                }catch(Exception ioe){                JOptionPane.showMessageDialog(null, "Deber un introducir el ID " +ioe);            }        }else if(e.getSource() == modificar){                        try{                int i = Integer.parseInt(JOptionPane.showInputDialog("ID de la teoria a modificar"));                ResultSet resultado = cn.buscar(i);                if(resultado.next()){                    String au = JOptionPane.showInputDialog("Autor");                    String an = JOptionPane.showInputDialog("Año");                    String cie = JOptionPane.showInputDialog("Ciencia");                     if(au.isEmpty()){                        JOptionPane.showMessageDialog(null, "Debes rellenar todos los campos");                                            }else if(an.isEmpty()){                        JOptionPane.showMessageDialog(null, "Debes rellenar todos los campos");                    }else if(cie.isEmpty()){                        JOptionPane.showMessageDialog(null, "Debes rellenar todos los campos");                    }else{                      cn.modificar(i, au, an, cie);                                          }                }            }catch(Exception ioe){                JOptionPane.showMessageDialog(null, "Error al modificar datos: " +ioe);            }                     }else if(e.getSource() == eliminar){            this.delectRows(tabla.getSelectedRows());                                                }else if(e.getSource() == seleccionar){            tabla.selectAll();        }            }        public void delectRows(int[] rowSelected){        for (int i = 0; i<rowSelected.length; i++){            String query = "DELETE FROM datos WHERE IDE="+lista.get(rowSelected[i]);            try{                cn.getSt().executeUpdate(query);            }catch(SQLException sqle){                JOptionPane.showMessageDialog(null, "Error al eliminar teoria " +sqle);            }        }    }     public void tableChanged(TableModelEvent tme) {                        }      }

arielb:
Hola, bueno revisando tu código lo que veo que puedes hacer es poner el código donde haces el select en un método e invocarlo nuevamente cuando lo necesites.
Existen otras formas mas óptimas pero tendrás que modificar bastante tú código.


--- Código: Java(TM) 2 Platform Standard Edition 5.0 ---public void cargarDatos(){        try{             ResultSet aux = cn.getSt().executeQuery("SELECT*FROM datos");             while(aux.next()){                              Object [] fila = {aux.getObject(1), aux.getObject(2), aux.getObject(3),                     aux.getObject(4), aux.getObject(5)};                                  dtm.addRow(fila);                                  lista.add((Integer)aux.getObject(5));             }                 }catch(SQLException ioe){                        }                }

Navegación

[0] Índice de Mensajes

Ir a la versión completa