hi..I am really new in Java software development..I am trying to make sure that the JXTable will refresh or update the data after the save button is clicked which means that the JXTable will display the lastest data that retrieved from the database..as what I had googled through the methods to solve my problem, I had tried for 3 different methods..but they aren't work as what I expected..Besides, I noticed that the JXTable cannot have duplicated of data..but I have duplicated of data..what is the method that can be used to solve this problem?for example (I want my JXTable to look like this),
Student Name Book Name
student A abc
student A def
student A ghi
student B abc
student B def
student B ghi
below is part of my code..

private void btnsaveActionPerformed(java.awt.event.ActionEvent evt) {                                        
        // TODO add your handling code here:
        Statement stmt = null;

        try
        {
            Class.forName("org.postgresql.Driver");
        }

        catch(java.lang.ClassNotFoundException e)
        {
            System.err.print("ClassNotFoundException: ");
            System.err.print(e.getMessage());
        }

        try
        {
            String connectionUrl = "jdbc:postgresql://localhost:5432/abc?user=root&password=pwd";
            Connection conn = DriverManager.getConnection(connectionUrl);

            Object studentName = comboxstudentname.getSelectedItem();
            Object bookName = comboxbookname.getSelectedItem();

            stmt = conn.createStatement();

            String strSQL = "INSERT INTO student (student_name, book_name) VALUES ('"+studentName+"', '"+bookName+"')";
            stmt.executeUpdate(strSQL);

            JOptionPane.showMessageDialog(null, "Save.");
            //revalidate();   //1st method that I had tried
            //repaint();      //2nd method that I had tried
            //jXTable1.tableChanged(new TableModelEvent(jXTable1.getModel()));   //3rd method that I had tried          
            stmt.close();
            conn.close();
        }

        catch(SQLException e)
        {
            System.out.println("SQL Exception: "+e.toString());
        }
    }

thanks in advance.

Recommended Answers

All 6 Replies

Anyone know how to solve this problem?or if I used JTable in this case, then what is the solution?thanks in advance.

I have no idea about JXTable, never used it, but updating data is no problem in the JTable component. You simply update the underlying model and fire a dataChanged event. Some methods of the default table model fire that event for you automatically.

Edit: I don't see where you actually update any data in the model used by your table. You need to re-query the table or simply add those fields directly into your table model. It looks like JXTable uses the same TableModel interface for its data.

I have no idea about JXTable, never used it, but updating data is no problem in the JTable component. You simply update the underlying model and fire a dataChanged event. Some methods of the default table model fire that event for you automatically.

Edit: I don't see where you actually update any data in the model used by your table. You need to re-query the table or simply add those fields directly into your table model. It looks like JXTable uses the same TableModel interface for its data.

thanks for the reply..but can you show some code or example on how to do this?thanks..

What are you using for a table model and how are you creating it?

I didn't create any table model. Is it a must to create a table model for my JTable?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.