问题
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