• Domingo 28 de Abril de 2024, 11:35

Autor Tema:  Refrescar JTABLE!!  (Leído 7125 veces)

DarkGhetto22

  • Nuevo Miembro
  • *
  • Mensajes: 2
    • Ver Perfil
Refrescar JTABLE!!
« en: Jueves 5 de Abril de 2012, 13:57 »
0
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
  1. import java.awt.event.ActionEvent;
  2. import java.awt.event.ActionListener;
  3. import java.sql.ResultSet;
  4. import java.sql.SQLException;
  5. import java.util.ArrayList;
  6. import java.util.List;
  7. import javax.swing.JFrame;
  8. import javax.swing.JMenuBar;
  9. import javax.swing.JMenu;
  10. import javax.swing.JMenuItem;
  11. import javax.swing.JOptionPane;
  12. import javax.swing.JTable;
  13. import javax.swing.JScrollPane;
  14. import javax.swing.table.DefaultTableModel;
  15. import javax.swing.event.TableModelEvent;
  16. import javax.swing.event.TableModelListener;
  17.  
  18. public class teoriasDatos extends JFrame implements ActionListener, TableModelListener{
  19.  
  20.     private final String[] titulos = {"Teoria", "Autor", "Fecha", "Ciencia", "Id"};
  21.    
  22.     private JMenuBar barra;
  23.     private JMenu archivo, edicion;
  24.     private JMenuItem salir, buscar, modificar, eliminar, seleccionar;
  25.    
  26.     private DefaultTableModel dtm = new DefaultTableModel();
  27.    
  28.     private JTable tabla = new JTable(dtm);
  29.    
  30.     private JScrollPane scroll = new JScrollPane(tabla);
  31.    
  32.     private List<Integer> lista = new ArrayList<Integer>();
  33.     conexion cn = new conexion();
  34.    
  35.     public teoriasDatos(){
  36.         super("Teorias System");
  37.         this.setLayout(null);
  38.         this.setSize(900, 460);
  39.         this.setResizable(false);
  40.         this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
  41.         this.Objetos();
  42.         this.setVisible(true);
  43.     }
  44.    
  45.    
  46.    
  47.     public void Objetos(){
  48.         barra = new JMenuBar();
  49.         archivo = new JMenu("Archivo");
  50.         edicion = new JMenu("Edicion");
  51.         buscar = new JMenuItem("Buscar");
  52.         modificar = new JMenuItem("Modificar");
  53.         eliminar = new JMenuItem("Eliminar");
  54.         seleccionar = new JMenuItem("Seleccionar");
  55.         salir = new JMenuItem("Salir");
  56.         barra.add(archivo);
  57.         barra.add(edicion);
  58.         archivo.add(salir);
  59.         edicion.add(buscar);
  60.         edicion.add(modificar);
  61.         edicion.add(eliminar);
  62.         edicion.addSeparator();
  63.         edicion.add(seleccionar);
  64.         this.setJMenuBar(barra);
  65.  
  66.  
  67.        
  68.        
  69.         dtm.setColumnIdentifiers(titulos);
  70.         lista.clear();
  71.        
  72.         try{
  73.              ResultSet aux = cn.getSt().executeQuery("SELECT*FROM datos");
  74.              while(aux.next()){
  75.              
  76.                  Object [] fila = {aux.getObject(1), aux.getObject(2), aux.getObject(3),
  77.                      aux.getObject(4), aux.getObject(5)};
  78.                  
  79.                  dtm.addRow(fila);
  80.                  
  81.                  lista.add((Integer)aux.getObject(5));
  82.              }
  83.              
  84.         }catch(SQLException ioe){
  85.       JOptionPane.showMessageDialog(null, "Error al leer teorias: " + ioe);
  86.         }
  87.  
  88.        
  89.         scroll.setBounds(0, 0, 900, 460);
  90.         this.add(scroll);
  91.        
  92.         salir.addActionListener(this);
  93.         buscar.addActionListener(this);
  94.         modificar.addActionListener(this);
  95.         eliminar.addActionListener(this);
  96.         seleccionar.addActionListener(this);
  97.         dtm.addTableModelListener(tabla);
  98.        
  99.        
  100.        
  101.     }
  102.     public void actionPerformed(ActionEvent e) {
  103.         if(e.getSource()==buscar){
  104.             try{
  105.                 int i = Integer.parseInt(JOptionPane.showInputDialog("ID de la teoria a buscar"));
  106.                 ResultSet resultado = cn.buscar(i);
  107.                 tabla.changeSelection(i-1, i, false, false);
  108.    
  109.             }catch(Exception ioe){
  110.                 JOptionPane.showMessageDialog(null, "Deber un introducir el ID " +ioe);
  111.             }
  112.         }else if(e.getSource() == modificar){
  113.            
  114.             try{
  115.                 int i = Integer.parseInt(JOptionPane.showInputDialog("ID de la teoria a modificar"));
  116.                 ResultSet resultado = cn.buscar(i);
  117.                 if(resultado.next()){
  118.                     String au = JOptionPane.showInputDialog("Autor");
  119.                     String an = JOptionPane.showInputDialog("Año");
  120.                     String cie = JOptionPane.showInputDialog("Ciencia");
  121.  
  122.                     if(au.isEmpty()){
  123.                         JOptionPane.showMessageDialog(null, "Debes rellenar todos los campos");
  124.                        
  125.                     }else if(an.isEmpty()){
  126.                         JOptionPane.showMessageDialog(null, "Debes rellenar todos los campos");
  127.                     }else if(cie.isEmpty()){
  128.                         JOptionPane.showMessageDialog(null, "Debes rellenar todos los campos");
  129.                     }else{
  130.                       cn.modificar(i, au, an, cie);
  131.                      
  132.                     }
  133.                 }
  134.             }catch(Exception ioe){
  135.                 JOptionPane.showMessageDialog(null, "Error al modificar datos: " +ioe);
  136.             }
  137.  
  138.            
  139.         }else if(e.getSource() == eliminar){
  140.             this.delectRows(tabla.getSelectedRows());
  141.            
  142.            
  143.                
  144.         }else if(e.getSource() == seleccionar){
  145.             tabla.selectAll();
  146.         }
  147.        
  148.     }
  149.    
  150.     public void delectRows(int[] rowSelected){
  151.         for (int i = 0; i<rowSelected.length; i++){
  152.             String query = "DELETE FROM datos WHERE IDE="+lista.get(rowSelected[i]);
  153.             try{
  154.                 cn.getSt().executeUpdate(query);
  155.             }catch(SQLException sqle){
  156.                 JOptionPane.showMessageDialog(null, "Error al eliminar teoria " +sqle);
  157.             }
  158.         }
  159.     }
  160.  
  161.     public void tableChanged(TableModelEvent tme) {
  162.            
  163.        
  164.     }
  165.      
  166. }

arielb

  • Moderador
  • ******
  • Mensajes: 771
  • Nacionalidad: pa
    • Ver Perfil
    • http://coder-pa.blogspot.com
Re:Refrescar JTABLE!!
« Respuesta #1 en: Jueves 5 de Abril de 2012, 17:04 »
0
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
  1. public void cargarDatos()
  2. {
  3.         try{
  4.              ResultSet aux = cn.getSt().executeQuery("SELECT*FROM datos");
  5.              while(aux.next()){
  6.              
  7.                  Object [] fila = {aux.getObject(1), aux.getObject(2), aux.getObject(3),
  8.                      aux.getObject(4), aux.getObject(5)};
  9.                  
  10.                  dtm.addRow(fila);
  11.                  
  12.                  lista.add((Integer)aux.getObject(5));
  13.              }
  14.              
  15.     }catch(SQLException ioe){
  16.                
  17.         }
  18.                
  19. }
"Porque de tal manera amó Dios al mundo que dio a su hijo unigénito para que todo aquél que en él crea no se pierda mas tenga vida eterna"
Juan 3:16

http://coder-pa.blogspot.com