javaswingjtabletablemodel

JTable will set to editable after converting ResultSet to TableModel with DbUtils. How to make it non-editable again?


Here is My Code for doing this`

public static void addSong(String[] fileDetail, JTable SongData_Table)
{
    try {
        con = DBConnection.getCon();
        stmt = con.createStatement();

        stmt.executeUpdate("insert into songs values (null,'" + fileDetail[0] + "', '" + fileDetail[1] + "',null,null)");
        ResultSet rs = stmt.executeQuery("select * from songs");

        TableModel model = DbUtils.resultSetToTableModel(rs);
        SongData_Table.setModel(model);

        if (con != null) {
            stmt.close();
            con.close();
        }
    } catch (SQLException e) {
        System.out.println("Error in Stmt " + e);
    }
}

Solution

  • Variable names should NOT start with an upper case character. SongData_Table should be songDataTable.

    Override the isCellEditable(...) method of the JTable, instead of the TableModel.

    JTable songDataTable = new JTable()
    {
        @Override boolean isCellEditatable(int row, int column)
        {
            return false;
        }
    };