在mysql数据库中插入数据后刷新JTable

时间:2013-07-29 18:39:37

标签: java mysql swing jtable jpanel

我在MySql中插入数据后尝试刷新de JTable,但我无法使其工作。

数据填写在agregar JPanel中并在点击按钮后保存,在mysql控制台中检查,数据被添加到数据库中,但是当我打开表时它不会更新,除非我重新启动程序。 / p>

这是我的代码:

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);

}

}

1 个答案:

答案 0 :(得分:0)

数据库更新后,您需要重新填充模型。添加刷新按钮,单击该按钮,更新contactTableModel