setValueAt
时间: 2024-01-04 14:19:47 浏览: 29
根据提供的引用内容,setValueAt()是一个方法,用于在TableModel中设置指定行和列的值。它接受三个参数:要设置的新值、行索引和列索引。该方法通常用于在表格中编辑单元格的值。
以下是一个示例,演示如何使用setValueAt()方法来设置表格中指定单元格的值:
```java
public class MyTableModel extends AbstractTableModel {
private Object[][] data;
private String[] columnNames;
public MyTableModel(Object[][] data, String[] columnNames) {
this.data = data;
this.columnNames = columnNames;
}
public int getRowCount() {
return data.length;
}
public int getColumnCount() {
return columnNames.length;
}
public Object getValueAt(int row, int col) {
return data[row][col];
}
public void setValueAt(Object aValue, int row, int col) {
data[row][col] = aValue;
fireTableCellUpdated(row, col);
}
public boolean isCellEditable(int row, int col) {
return true;
}
}
// 使用示例
Object[][] data = {
{"John", "Doe", 30},
{"Jane", "Smith", 25},
{"Bob", "Johnson", 35}
};
String[] columnNames = {"First Name", "Last Name", "Age"};
MyTableModel model = new MyTableModel(data, columnNames);
JTable table = new JTable(model);
// 设置第一行第一列的值为"New Value"
model.setValueAt("New Value", 0, 0);
```
这个示例演示了如何创建一个自定义的TableModel,并使用setValueAt()方法来设置表格中指定单元格的值。在这个示例中,我们创建了一个包含姓名和年龄的表格,并将第一行第一列的值设置为"New Value"。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)