在数据库中插入新数据时刷新我的JTable

时间:2019-06-11 12:42:14

标签: java jtable

我想在插入数据时刷新JTable。

这是JFrame的构造函数:

 public MyWindowGridLayout()throws Exception{

    super("Dashboard Agence la DS");
    setFont(new Font("Avenir", Font.PLAIN, 12));
    this.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
    this.setSize(950,550);
    this.setResizable(false);
    this.setLocationRelativeTo(null);

    JPanel content = (JPanel) this.getContentPane();

    content.add(createEventPanel(), BorderLayout.WEST);

    content.add(createListDB());

    content.add(createRightPanel(), BorderLayout.EAST);

    content.add(createStatusBar(), BorderLayout.SOUTH);

}

列表的JPanel:

public JScrollPane createListDB()throws Exception{

    JPanel lista = new JPanel();
    Class.forName("com.mysql.jdbc.Driver");
    Connection connection = DriverManager.getConnection(url, userid, password);
    String sql = "SELECT * FROM events";
    Statement statement = connection.createStatement();
    ResultSet rs = statement.executeQuery(sql);
    while(rs.next())
    {
        int i = rs.getInt(1);
        String d = rs.getString(2);
        model.addRow(new Object[]{i,d});
    }

    model.setColumnIdentifiers(columns);

    Font font = new Font("Avenir",1,18);
    table.setFont(font);
    table.setRowHeight(30);

    table.setModel(model);

    JScrollPane scrollPane = new JScrollPane(table);

    return scrollPane;
}

添加方法:

public void addEvent()throws Exception{

    // DEFINITION DU LAYOUT
    pane = new JPanel();
    pane.setLayout(new GridLayout(0, 2, 2, 2));


    //  AJOUT 3 CHAMPS DE TEXTE AU PANEL
    nameField = new JTextField(20);
    placeField = new JTextField(20);
    dateField = new JTextField(20);

    pane.add(new JLabel("NOM"));
    pane.add(nameField);

    pane.add(new JLabel("LIEU"));
    pane.add(placeField);

    pane.add(new JLabel("DATE (AAAA-MM-JJ)"));
    pane.add(dateField);

    // FENETRE DE DIALOGUE DAJOUT A LA BASE DE DONNEES

    int option = JOptionPane.showConfirmDialog(frame, pane, "Ajout d'évenement", JOptionPane.OK_CANCEL_OPTION, JOptionPane.INFORMATION_MESSAGE);

    if (option == JOptionPane.OK_OPTION) {

        String event = nameField.getText();

        pane = new JPanel();
        pane.setLayout(new BoxLayout(pane, BoxLayout.PAGE_AXIS));

        pane.add(new JLabel(event + " ajouté a la base de données ! "));

        JOptionPane.showMessageDialog(frame, pane);
// QUERY

        try{
            Class.forName("com.mysql.jdbc.Driver");
            Connection connection = DriverManager.getConnection(url, userid, password);
            String sql = "INSERT INTO events (name,place,date) VALUES('" + nameField.getText() + "','" + placeField.getText() + "','" + dateField.getText() + "')";
            System.out.println(sql);

            try (Statement statement = connection.createStatement()){
                statement.executeUpdate(sql);
            }
        }
        catch(Exception e){
            e.printStackTrace();
        }
    }
}

我不知道该怎么办。我已经尝试过revalidate()方法,repaint(),fireTableChanged(),但是这些都不起作用。 谢谢大家!

0 个答案:

没有答案