我正在使用JTable
具有 4 列的如何设置JTable
来选择一行,但用户不应更改该行的值?
Jtable.setEnabled(false);
该语句对我不起作用,因为用户无法选择该行。
如何用您的答案替换此代码?
public void setModel()
{
String[] colNames = {"Name","Email","Department","Status"};
TableModel model = new DefaultTableModel(colNames,500);
table.setModel(model);
String insert = "select * from " + deptName;
try
{
conn = ac.getConnection();
stmt = conn.prepareStatement(insert);
rs = stmt.executeQuery();
int row = 0;
while(rs.next())
{
String[] rowData = new String[5];
for(int i=1;i<=4;i++)
{
rowData[i-1] = rs.getString(i);
}
model.setValueAt(rowData[0], row, 0);
model.setValueAt(rowData[1], row, 1);
model.setValueAt(rowData[2], row, 2);
model.setValueAt(rowData[3], row, 3);
row++;
}
}catch(SQLException s){}
catch(ClassNotFoundException e)
{e.printStackTrace();}
finally
{
try
{
if(rs != null){rs.close();}
if(stmt != null){stmt.close();}
if(conn != null) {conn.close();}
}
catch(SQLException e){}
}
}