私のアプリケーションには 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 などのメソッドを使用できないため、上記のコードで変更します。しかし、上記のコードではデータ (行) を明示的に挿入できません。助けてください。