在我的应用程序中有一个 JTable,我想在创建表后插入行。
以下所有代码都在框架的构造函数中。
代码:
private TableModel model = new AbstractTableModel(){
String[] columnNames = {"First Name","Last Name","Sport","# of Years","Vegetarian"};
private Object[][] data = {};
public int getColumnCount() {
return columnNames.length;
}
public int getRowCount() {
return data.length;
}
public String getColumnName(int col) {
return columnNames[col];
}
public Class getColumnClass(int c) {
return getValueAt(0, c).getClass();
}
public Object getValueAt(int row, int col) {
return data[row][col];
}
public boolean isCellEditable(int row, int col) {
retuen false;
}
public Class getColumnClass(int c) {
return getValueAt(0, c).getClass();
}
};
table = new JTable(model);
table.setPreferredScrollableViewportSize(new Dimension(500, 70));
table.setFillsViewportHeight(true);
JScrollPane scrollPane = new JScrollPane(table);
scrollPane.setBounds(5, 218, 884, 194);
//now adding this to the frame where I want to show
frame.add(scrollPane);
现在我想在表中插入行或数据。这怎么可能。以前我使用DefaultTableModel但我们不能在 DefaultTableModel 中使用 isCellEditable 和其他方法,所以我用上面的代码更改了它。但在上面的代码中,我无法明确插入数据(行),请帮助我。