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

て烟熏妆下的殇ゞ 提交于 2019-11-27 07:30:39

问题


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);
    }
}

回答1:


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;
    }
};



回答2:


In DbUtils.resultSetToTableModel, replace

return new DefaultTableModel(rows, columnNames);

and return a TableModel that overrides isCellEditable():

return new DefaultTableModel(rows, columnNames){

    @Override
    public boolean isCellEditable(int row, int column) {
        return false;
    }
};



回答3:


You could override the method isCellEditable:

 //instance table model
  DefaultTableModel tableModel = new DefaultTableModel() {

  @Override
  public boolean isCellEditable(int row, int column) {
   //all cells false
   return false;
   }
 };

  table.setModel(tableModel);

or....

  table.setEnabled(false);


来源:https://stackoverflow.com/questions/25943556/jtable-will-set-to-editable-after-converting-resultset-to-tablemodel-with-dbutil

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!