2013-07-29 167 views
0

在MySql中插入數據後,我一直試圖刷新de JTable,但我無法設法使其工作。在MySQL數據庫中插入數據後刷新JTable

數據填充到agregar JPanel中並保存後點擊一個按鈕,檢查mysql控制檯,數據被添加到數據庫中,但是當我打開表格時它不會更新,除非我重新啓動程序。

這是我的代碼:

public class verTodos extends JPanel { 

    contactoDAO dao = new contactoDAO(); 
    List<contacto> contactos = dao.getAll(); 
    private JTable table; 

    public verTodos() {   
      String[] colName = { "Id", "Nombre", "Apellido", "Telefono", 
       "Direccion", "Email", "Rubro1", "Rubro2", "Rubro3" }; 
     Object[][] obj = new Object[contactos.size()][10];   
     for (int i = 0; i < contactos.size(); i++) { 
      contacto c = contactos.get(i);  
      obj[i][0] = c.getId(); 
      bj[i][1] = c.getNombre(); 
      obj[i][2] = c.getApellido(); 
      obj[i][3] = c.getTelefono(); 
      obj[i][4] = c.getDireccion(); 
      obj[i][5] = c.getEmail(); 
      obj[i][6] = c.getRubro1(); 
      obj[i][7] = c.getRubro2(); 
      obj[i][8] = c.getRubro3();  
      setBounds(100, 100, 800, 300);       
     } 
     DefaultTableModel contactTableModel = new DefaultTableModel(obj,colName);   
     table = new JTable(contactTableModel);   
     setLayout(new BorderLayout(0, 0));  
     contactTableModel.fireTableDataChanged(); 
     contactTableModel.setColumnIdentifiers(colName);    
      add(table.getTableHeader(), BorderLayout.NORTH); 
     JScrollPane scrollPane = new JScrollPane(); 
     add(scrollPane, BorderLayout.CENTER); 
     scrollPane.setViewportView(table); 
     repaint();  
    }  
} 

這是包含JPanels主屏:

public class principal extends JFrame { 

JFrame frame = new JFrame(); 
JPanel container = new JPanel(); 
CardLayout cl = new CardLayout(); 
private final JMenuBar menuBar = new JMenuBar(); 

/..... 
..... 

private final JMenuItem mntmVerTodos = new JMenuItem("Ver todos"); 
    private final JMenuItem mntmAgregar = new JMenuItem("Agregar"); 
agregar ag = new agregar(); 
verTodos ver = new verTodos(); 
... 
    ... 


public static void main(String[] args) { 
    SwingUtilities.invokeLater(new Runnable() { 
     public void run() { 
      new principal(); 

     } 
    }); 

} 


public principal() { 

    textField.setMaximumSize(new Dimension(300, 500)); 
    textField.setBounds(new Rectangle(0, 0, 30, 0)); 
    textField.setColumns(10); 

    Dimension screen = Toolkit.getDefaultToolkit().getScreenSize(); 
    int X = (screen.width/2) - (1366/2); 
    int Y = (screen.height/2) - (720/2); 

    frame.setBounds(X, Y, 1363, 720); 

    container.setLayout(cl); 
    ver.setComponentOrientation(ComponentOrientation.LEFT_TO_RIGHT); 

    container.add(ver, "ver"); 
      container.add(ag, "ag"); 

    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
    setBounds(100, 100, 450, 300); 

    frame.getContentPane().add(container); 

    frame.setJMenuBar(menuBar); 

    menuBar.add(mnNewMenu); 
      cl.show(container, "ver"); 

    mntmVerTodos.addActionListener(new ActionListener() { 
     public void actionPerformed(ActionEvent e) { 

      cl.show(container, "ver"); 


     } 
    }); 

    mnContactos.add(mntmVerTodos); 





    mnContactos.add(mntmAgregar); 
    mntmModificar.addActionListener(new ActionListener() { 
     public void actionPerformed(ActionEvent e) { 
      cl.show(container, "mod"); 

     } 
    }); 



    menuBar.add(btn); 

    frame.setVisible(true); 

} 

}

+0

請學習java命名約定並堅持使用它們。 – kleopatra

回答

0

你需要重新裝載您的模型一旦數據庫已更新。添加一個刷新按鈕,當點擊時,更新contactTableModel

+0

我希望它在數據添加到數據庫時自動刷新,這是在另一個JPanel中添加的,所以當我回到verTodos JPanel時,它已經更新了。正如我一直在看我意識到的問題是,當我回到它時,JPanel不會刷新 – user2623908

+0

如果你想自動刷新,你可以創建一個線程監視數據庫並尋找是否有任何如果是這樣,更新表格。如果你在某個地方上傳完整的項目,我可以進一步提供幫助。 – JBuenoJr

+0

感謝回答這麼快,我剛剛編輯與面板容器的帖子,有沒有什麼辦法讓我的點擊JmenuItem mntmVerTodos verTodos面板重新加載? – user2623908

相關問題