Search code examples
javaswingjtableresultset

How to Add ResultSet Data To JTable with out changing Default Colomn names of jTable


I have the following class:

public class customer_master extends javax.swing.JInternalFrame {

    Connection con = null;
    PreparedStatement ps = null;
    ResultSet rs = null;

    JFrame parent;
    public customer_master(JFrame parent) {

        this.parent=parent;
        initComponents();

        try {
            String qry="select customer_code, customer_name, customer_address, customer_created_time from customer";
            database.JavaConnect jc= new JavaConnect(); 
            con = jc.ConnectDB();
            ps = con.prepareStatement(qry);
            rs = ps.executeQuery();
            jTable1.setModel(DbUtils.resultSetToTableModel(rs));
            con.close();
        }
        catch(SQLException e)
        {
            JOptionPane.showMessageDialog(null, e);
        }
    }    
}

It also change the name of columns but I want to add rs without changing columns name please if any one can help. Thanks in advance.


Solution

  • Your question implies that you are refeshing and existing JTable with a new ResultSet. So when you create the JTable the first time and assign names to each column you need to add:

    JTable table = new JTable(...);
    table.setAutoCreateColumnsFromModel( false );
    

    This will tell the table not to recreate the TableColumnModel so all the columns and custom renderers and editors will remain.