Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: Refreshing JTable!!

  1. #1
    Junior Member
    Join Date
    Apr 2012
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Refreshing JTable!!

    Hi, I hope you are fine, I got a doubt.......it's just that I have a defaulttablemodel and jtable where I get information from my DB, the problem is that I got another windows to add the information, it's a windows with a JTextField and JLabel and button...well my problem is that when I add information to the DB my "QUERY" runs without any problem, but I don't know how to do for refreshing my JTable because if I have my JTable opened, it doesn't refresh, and when I close it and I open it again, I get all the new informations

    I've been trying with a lots of methods like:
    fireTableDataChanged();
    .fireTableStructureChanged();
    .updateUI();
    .revalidate();

    But nothing, it seems to work, I don't know why :S
    this is my code, if anybody has a suggetion, pls let me know and I will really thank you:

    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)};
                     lista.add((Integer)aux.getObject(5));
                     dtm.addRow(fila);
     
                 }
     
            }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());
                tabla.clearSelection();
     
            }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) {
     
        }
     
    }


  2. #2

    Default Re: Refreshing JTable!!

    You should update the default table model. I don't see where you do that in your code.

Similar Threads

  1. Refreshing from teh main class
    By lorider93p in forum What's Wrong With My Code?
    Replies: 6
    Last Post: February 9th, 2012, 11:12 AM
  2. [SOLVED] Refreshing Variables Displayed on a JPanel
    By StandbyExplosion in forum What's Wrong With My Code?
    Replies: 6
    Last Post: August 24th, 2011, 06:14 PM
  3. [SOLVED] Refreshing a JTabbedPane?
    By Hallowed in forum Java Theory & Questions
    Replies: 2
    Last Post: May 31st, 2011, 01:02 PM
  4. refreshing a jtable with database
    By kollyisrealisaac in forum JDBC & Databases
    Replies: 0
    Last Post: May 7th, 2011, 01:50 PM
  5. refreshing JEditorPane
    By nasi in forum AWT / Java Swing
    Replies: 9
    Last Post: April 9th, 2010, 04:01 AM

Tags for this Thread