hello all, I created a program that connected to database!.... I've done to display all records from database in JTable..... but when I'm deleting a row it can't delete automatically it needs to close the program to update the JTable!....

here's my program!....

package classes;

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.sql.*;

public class CourseView extends JFrame implements ActionListener {

    private static final long serialVersionUID = 1L;

    private JPanel jContentPane = null;

    private JScrollPane jScrollPane = null;

    private JTable jTable = null;

    private JButton jButton = null;

    private JButton jButton1 = null;

    private JButton jButton2 = null;

    String[] columnName;
    Object[][] cells;
    ResultSet rs = null;
    Connection con = null;
    Statement stmt = null;
    ResultSetMetaData metaData = null;
    /**
     * This method initializes jScrollPane  
     *  
     * @return javax.swing.JScrollPane  
     */
    private JScrollPane getJScrollPane() {
        if (jScrollPane == null) {
            jScrollPane = new JScrollPane();
            jScrollPane.setBounds(new Rectangle(1, 1, 442, 234));
            jScrollPane.setViewportView(getJTable());
        }
        return jScrollPane;
    }

    /**
     * This method initializes jTable   
     *  
     * @return javax.swing.JTable   
     */
    private JTable getJTable() {
        if (jTable == null) {
            fillTable();
            jTable = new JTable(cells,columnName);
            jTable.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
        }
        return jTable;
    }

    /**
     * This method initializes jButton  
     *  
     * @return javax.swing.JButton  
     */
    private JButton getJButton() {
        if (jButton == null) {
            jButton = new JButton();
            jButton.setText("New");
            jButton.setBounds(new Rectangle(97, 244, 72, 20));
            jButton.setActionCommand("new");
            jButton.addActionListener(this);
        }
        return jButton;
    }

    /**
     * This method initializes jButton1 
     *  
     * @return javax.swing.JButton  
     */
    private JButton getJButton1() {
        if (jButton1 == null) {
            jButton1 = new JButton();
            jButton1.setText("Modify");
            jButton1.setBounds(new Rectangle(177, 244, 72, 20));
            jButton1.setActionCommand("edit");
            jButton1.addActionListener(this);
        }
        return jButton1;
    }

    /**
     * This method initializes jButton2 
     *  
     * @return javax.swing.JButton  
     */
    private JButton getJButton2() {
        if (jButton2 == null) {
            jButton2 = new JButton();
            jButton2.setText("Delete");
            jButton2.setBounds(new Rectangle(257, 244, 72, 20));
            jButton2.setActionCommand("delete");
            jButton2.addActionListener(this);
        }
        return jButton2;
    }

    /**
     * @param args
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                CourseView thisClass = new CourseView();
                thisClass.setDefaultCloseOperation(thisClass.EXIT_ON_CLOSE);
                thisClass.setVisible(true);
            }
        });
    }

    /**
     * This is the default constructor
     */
    public CourseView() {
        super();
        try{
            Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
            con = DriverManager.getConnection("jdbc:odbc:enrollmentData");
        }catch(ClassNotFoundException cl){
            System.out.print("Class not found: "+cl.getMessage());
        }catch(SQLException sqle){
            System.out.print("SQLException: "+sqle.getMessage());
        }
        initialize();

    }

    /**
     * This method initializes this
     * 
     * @return void
     */
    private void initialize() {
        this.setSize(450, 300);
        this.setContentPane(getJContentPane());
        this.setTitle("Courses");
        this.setResizable(false);
        this.addWindowListener(new WindowAdapter(){
            public void windowOpened(WindowEvent e){
                jTable.selectAll();
            }
            public void windowClosing(WindowEvent e){
                try{
                    rs.close();
                    stmt.close();
                    con.close();    
                }catch(SQLException sqle){
                    System.out.print("Error: "+sqle.getMessage());
                }
                CourseView courseview = new CourseView();
                courseview.dispose();
            }
        });
    }

    /**
     * This method initializes jContentPane
     * 
     * @return javax.swing.JPanel
     */
    private JPanel getJContentPane() {
        if (jContentPane == null) {
            jContentPane = new JPanel();
            jContentPane.setLayout(null);
            jContentPane.add(getJScrollPane(), null);
            jContentPane.add(getJButton(), null);
            jContentPane.add(getJButton1(), null);
            jContentPane.add(getJButton2(), null);
        }
        return jContentPane;
    }

    public void actionPerformed(ActionEvent e){

        AddCourse course = new AddCourse();
        if(e.getActionCommand().equals("new")){
            course.setVisible(true);
            course.setDefaultCloseOperation(course.DISPOSE_ON_CLOSE);
            this.dispose();
        }else if(e.getActionCommand().equals("edit")){
            course.setVisible(true);
            course.jTextField.setText((String)jTable.getValueAt(jTable.getSelectedRow(),0));
            course.jTextField.setEditable(false);
            course.jTextArea.setText((String)jTable.getValueAt(jTable.getSelectedRow(),1));
            this.dispose();
        }else if(e.getActionCommand().equals("delete")){
            try{
                stmt = con.createStatement();
                int confirm = JOptionPane.showConfirmDialog(null,"Are you sure you want delete?","Confirm",JOptionPane.YES_NO_OPTION);
                if(confirm==0){
                    stmt.executeUpdate("delete from course where CourseCode = '"+ jTable.getValueAt(jTable.getSelectedRow(),0) +"'");
                }else{

                }
            }catch(SQLException sqle){
                sqle.getMessage();
            }

        }
    }

    public void fillTable(){
        try{
            stmt = con.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_READ_ONLY);
            rs = stmt.executeQuery("select * from course");
            metaData = rs.getMetaData();
            columnName = new String[metaData.getColumnCount()];
            for(int i=0;i<metaData.getColumnCount();i++){
                columnName[i] = metaData.getColumnName(i+1);
            }
            rs.last();
            int row = rs.getRow();
            rs.beforeFirst();
            int index = 0;
            cells = new Object[row][metaData.getColumnCount()];
            while(rs.next()){
                cells[index][0] = rs.getString(1);
                cells[index][1] = rs.getString(2);
                index++;
            }
        }catch(SQLException sqle){
            System.out.print("Error: "+sqle.getMessage() );
        }
    }


}

Just call DefaultTableModel.removeRow(int) on the TableModel. You will need to cast it to DefaultTableModel to call that method, since it is not part of the TableModel interface.

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.