Java - ACTUALIZAR JTABLE!!!

 
Vista:

ACTUALIZAR JTABLE!!!

Publicado por DarkGhetto22 (5 intervenciones) el 31/03/2012 04:22:03
Quisiera saber como podria hacer para actualizar un JTABLE luego de haber modifacado los datos de una Base de Datos...........he intendado con varios metodos como:

.repaint();
.updateui
.revalidate

todo esto la tabla, lo mas extrano es que tambien intentente con los metodos del DefaultTableModel y tampoco me actualiza la tabla....aca les dejo el codigo, gracias por todo:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.ResultSet;
import java.sql.SQLException;
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;
public class teoriasDatos extends JFrame implements ActionListener{
 
    private final String[] titulos = {"Id", "Teoria", "Autor", "Año", "Ciencia"};
 
    private JMenuBar barra;
    private JMenu archivo, edicion;
    private JMenuItem salir, buscar, modificar, eliminar;
    private DefaultTableModel dtm = new DefaultTableModel();
    private JTable tabla = new JTable(dtm);
    private JScrollPane scroll = new JScrollPane(tabla);
    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");
        salir = new JMenuItem("Salir");
        barra.add(archivo);
        barra.add(edicion);
        archivo.add(salir);
        edicion.add(buscar);
        edicion.add(modificar);
        edicion.add(eliminar);
        this.setJMenuBar(barra);
 
 
        dtm.setColumnCount(0);
        dtm.setColumnIdentifiers(titulos);
        dtm.setRowCount(0);
 
        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);
 
             }
 
        }catch(SQLException ioe){
      JOptionPane.showMessageDialog(null, "Error al leer registro: " + ioe);
        }
 
 
        scroll.setBounds(0, 0, 900, 460);
        this.add(scroll);
        salir.addActionListener(this);
        buscar.addActionListener(this);
        modificar.addActionListener(this);
        eliminar.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");
 
            cn.modificar(i, au, an, cie);
 
            //ACA ES DONDE ESTA MI PROBLEMA
            dtm.fireTableRowsInserted(i, i);
                }
            }catch(Exception ioe){
                JOptionPane.showMessageDialog(null, "Error al modificar datos: " +ioe);
            }
 
 
 
 
        }
 
    }
 
 
}
Valora esta pregunta
Me gusta: Está pregunta es útil y esta claraNo me gusta: Está pregunta no esta clara o no es útil
0
Responder

ACTUALIZAR JTABLE!!!

Publicado por Benito Martín Gutiérrez (11 intervenciones) el 01/04/2012 07:07:15
hum..
dtm.fireTableRowsInserted(i, i);
te has olvidado de ponerle un 1 o es asi?
Valora esta respuesta
Me gusta: Está respuesta es útil y esta claraNo me gusta: Está respuesta no esta clara o no es útil
0
Comentar