java jtable不可编辑,如何使jtable在java中不可编辑?

I created a function which loads data into a JTable. Everything's working fine except that all the cells in this table are editable. Btw, I used defaultTableModel for the table model. Im doing this in Netbeans IDE. Please help. Here's my code:

private void updateTable(String searchText){

if(searchText != null)

this._sqlCmd = this._sqlCmd + " WHERE "+columnCombo.getSelectedItem()+" LIKE '%"+searchText+"%'";

jTable1.setSurrendersFocusOnKeystroke(true);

table = (javax.swing.table.DefaultTableModel) jTable1.getModel();

try{

table.setRowCount(0);

}catch(Exception e){}

try {

ResultSet rs = conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_READ_ONLY).executeQuery(_sqlCmd);

while (rs.next()){

Object[] data = new Object[numOfCols];

for(int i=0; i

data[i] = rs.getObject(i+1);

}

table.addRow(data);

}

table.fireTableDataChanged();

} catch (SQLException ex) {

Logger.getLogger(FindContactGrid.class.getName()).log(Level.SEVERE, null, ex);

}

}

解决方案

private TableModel model = new DefaultTableModel(data, columnNames)

{

public boolean isCellEditable(int row, int column)

{

return false;//This causes all cells to be not editable

}

};

private JTable table = new JTable(model);

Edited.

If you are doing this in Netbeans IDE designer, follow the steps below:

Select the form on which the JTable is placed

From the Navigation Pane, expand JScrollPane and right-click on JTable and Select Customize Code as shown below:

Ff5DN.png

On the code customizer, select the second drop down and choose custom property. This enables you to edit the DefaultTableModel code definition.

Now paste this:

{public boolean isCellEditable(int row, int column){return false;}} before the last closing blacket );

Your final setup should look as shown below:

Press ok to save - and job done.

8CAjf.png

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值