对象[][] 数据 = {{1,2,3},{2,3,4},{4,5,6},{7,8,9}}; 字符串[] title= {Column1”, Column2”, Column3”}; JTable 示例JTable = new JTable (数据、列名)}; |
System.out.println(exampleJTable.getValueAt(2, 2)); |
TableModel byDefault = 例子JTable.getModel(); System.out.println(exampleJTable.getValueAt(2, 2).toString()); |
class Model 扩展 AbstractTableModel{此代码将模板与 table
private Object[][] 数据;
private String[] 标题;
public Model(Object[][] data, String[] title){
this.data = 数据;
this.title = 标题;
}
/**
* 返回列标题和 index
*/
public String getColumnName(int col) {
return this.title[col];
}
/**
* 返回列数
*/
public int getColumnCount() {
return this.title.length;
}
/**
* 返回行数
*/
public int getRowCount() {
return this.data.length;
}
/**
* 返回行和列交集处的对象
*/
public 对象 getValueAt(int row, int col) {
return this.data[row][col];
}
/**
* 修改行列交集处的对象
*/
public void setValueAt(Object value, int row, int col) {
this.data[row][col] = value;
}
}
//创建JTable JTable 表 = new JTable(); //创建模板 Model =new Model(data, title); //将模板链接到 JTable table.setModel(模型); |
Please disable your ad blocker and refresh the window to use this website.