我将向您粘贴我是如何使用 AbstractTableModel 填充表格的。
public class InventoryModel extends AbstractTableModel {
private static final String[] columnNames = { "Owner", "Location", "Sample Name", "Form Factor" };
private List<Samples> samplesList;
public InventoryModel(){
samplesList = SampleQueries.getAvailableSamples();
}
@Override
public int getColumnCount() {
// TODO Auto-generated method stub
return columnNames.length;
}
@Override
public int getRowCount() {
return samplesList.size();
}
@Override
public String getColumnName(int column) {
return columnNames[column];
}
@Override
public Object getValueAt(int rowIndex, int columnIndex) {
switch (columnIndex) {
case 0:
return samplesList.get(rowIndex).getCurrentOwner();
case 1:
return samplesList.get(rowIndex).getSampleLocation();
case 2:
return samplesList.get(rowIndex).getSampleName();
case 3:
return samplesList.get(rowIndex).getFormFactor();
}
return null;
}
这就是我在小组中的称呼。
table = new JTable();
model = new InventoryModel();
model.fireTableDataChanged();
panel.setLayout(new BorderLayout(0, 0));
table.setModel(model);
JScrollPane scroll = new JScrollPane(table);
panel.add(scroll);
希望它对你有用