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:
我创建了一个将数据加载到JTable的函数。除了该表中的所有单元格都是可编辑的之外,其他一切都运行良好。顺便说一下,我使用defaultTableModel作为表模型。我在Netbeans IDE中这样做。请帮助。这是我的代码:
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.length; 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);
}
}
5 个解决方案
#1
27
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:
编辑。如果您正在Netbeans IDE designer中进行此操作,请遵循以下步骤:
- Select the form on which the JTable is placed
- 选择放置JTable的表单
- From the Navigation Pane, expand JScrollPane and right-click on JTable and Select Customize Code as shown below:
- 从导航窗格中展开JScrollPane,右键单击JTable,选择Customize代码,如下所示:
- On the code customizer, select the second drop down and choose custom property. This enables you to edit the DefaultTableModel code definition.
- 在代码定制器上,选择第二个下拉并选择custom属性。这使您能够编辑DefaultTableModel代码定义。
- Now paste this:
{public boolean isCellEditable(int row, int column){return false;}}
before the last closing blacket ); - 现在粘贴这个:{公共布尔型isCellEditable(int row, int列){返回false;}}在最后关闭blacket之前;
Your final setup should look as shown below:
最后的设置应该如下所示:
- Press ok to save - and job done.
- 按ok保存-完成任务。
#2
6
If you use DefaultTableModel you can override the method isCellEditable and implement it when constructing GUI:
如果您使用DefaultTableModel,您可以重写方法isCellEditable,并在构建GUI时实现它:
table.setModel(new DefaultTableModel() {
@Override
public boolean isCellEditable(int row, int column) {
return false;
}
});
#3
2
Using Netbeans ANOTHER WAY is possible. if you want to continue using the default table model as the OP mentions It's not necessary to create a new table model if you don't want to.
使用Netbeans也有可能。如果您想继续使用默认的表模型,就像OP提到的那样,如果您不想创建一个新的表模型,那就没有必要了。
-
Select the JTable "properties"
选择JTable“属性”
-
Select the "TableModel" field in "properties" which will open another DialogBox.
在“属性”中选择“TableModel”字段,该字段将打开另一个对话框。
-
From there it is possible to modify the "editable" checkbox field for each column.
从这里可以修改每个列的“可编辑”复选框字段。
Not sure from which version this starts but I'm using Netbeans 7.2
不确定从哪个版本开始,但我使用Netbeans 7.2
#4
1
As other said you have to create you own DefaultTableModel
and override isCellEditable
. In order to use it in Netbeans designer :
正如其他人所说,您必须创建自己的DefaultTableModel并覆盖isCellEditable。为了在Netbeans设计者中使用:
- Right click on your table
- 右键单击您的表
- Properties -> Code
- 属性- >代码
- In Custom Creation Code add this :
new JTable(new MyModel())
(assuming you create class MyModel extends AbstractTableModel) - 在自定义创建代码中添加以下内容:new JTable(new MyModel())(假设您创建了类MyModel,扩展了AbstractTableModel)
#5
0
Try This
试试这个
JTable table = new JTable();
table.setEnabled(false);
#1
27
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:
编辑。如果您正在Netbeans IDE designer中进行此操作,请遵循以下步骤:
- Select the form on which the JTable is placed
- 选择放置JTable的表单
- From the Navigation Pane, expand JScrollPane and right-click on JTable and Select Customize Code as shown below:
- 从导航窗格中展开JScrollPane,右键单击JTable,选择Customize代码,如下所示:
- On the code customizer, select the second drop down and choose custom property. This enables you to edit the DefaultTableModel code definition.
- 在代码定制器上,选择第二个下拉并选择custom属性。这使您能够编辑DefaultTableModel代码定义。
- Now paste this:
{public boolean isCellEditable(int row, int column){return false;}}
before the last closing blacket ); - 现在粘贴这个:{公共布尔型isCellEditable(int row, int列){返回false;}}在最后关闭blacket之前;
Your final setup should look as shown below:
最后的设置应该如下所示:
- Press ok to save - and job done.
- 按ok保存-完成任务。
#2
6
If you use DefaultTableModel you can override the method isCellEditable and implement it when constructing GUI:
如果您使用DefaultTableModel,您可以重写方法isCellEditable,并在构建GUI时实现它:
table.setModel(new DefaultTableModel() {
@Override
public boolean isCellEditable(int row, int column) {
return false;
}
});
#3
2
Using Netbeans ANOTHER WAY is possible. if you want to continue using the default table model as the OP mentions It's not necessary to create a new table model if you don't want to.
使用Netbeans也有可能。如果您想继续使用默认的表模型,就像OP提到的那样,如果您不想创建一个新的表模型,那就没有必要了。
-
Select the JTable "properties"
选择JTable“属性”
-
Select the "TableModel" field in "properties" which will open another DialogBox.
在“属性”中选择“TableModel”字段,该字段将打开另一个对话框。
-
From there it is possible to modify the "editable" checkbox field for each column.
从这里可以修改每个列的“可编辑”复选框字段。
Not sure from which version this starts but I'm using Netbeans 7.2
不确定从哪个版本开始,但我使用Netbeans 7.2
#4
1
As other said you have to create you own DefaultTableModel
and override isCellEditable
. In order to use it in Netbeans designer :
正如其他人所说,您必须创建自己的DefaultTableModel并覆盖isCellEditable。为了在Netbeans设计者中使用:
- Right click on your table
- 右键单击您的表
- Properties -> Code
- 属性- >代码
- In Custom Creation Code add this :
new JTable(new MyModel())
(assuming you create class MyModel extends AbstractTableModel) - 在自定义创建代码中添加以下内容:new JTable(new MyModel())(假设您创建了类MyModel,扩展了AbstractTableModel)
#5
0
Try This
试试这个
JTable table = new JTable();
table.setEnabled(false);