次の構文を使用してテーブルを表示する Java GUI を作成しました。
table = new JTable(new MyTableModel(columnNames,
updateTable(cmbAdversary.getSelectedItem().toString(),
cmbdataType.getSelectedItem().toString())));
ここで、columnNames は文字列のベクトルです。cmbadversary と smbdataType はコンボ ボックスの選択です。
updateTable は、次のようにコンボ ボックスの選択に応じて Vector の Vector を返すメソッドです。
static Vector updateTable(String FilterVal1 , String FilterVal2)
{
try {
myVector = tssc.testSeverityFunctionService(FilterVal1,FilterVal2);
} catch (Exception e) {
e.printStackTrace();}
return myVector;
}
これは、AbstractTableModel を拡張する私のカスタム クラス MyTableModel がどのように見えるかです。
class MyTableModel extends AbstractTableModel
{
Vector columnNames = new Vector();
Vector Fdb = new Vector();
public MyTableModel(Vector cName,Vector rName){
this.columnNames = cName;
this.Fdb = rName;}
public int getColumnCount() { // number of columns in the model.
return columnNames.size();
}
public int getRowCount() { // number of rows in the model.
return Fdb.size();
}
@Override
public String getColumnName(int col) {
return columnNames.get(col).toString();
}
public Object getValueAt(int row, int col) {
Vector v = (Vector) this.Fdb.get(row);
return v.get(col);
}
@Override
public Class getColumnClass(int c) {
Vector v = (Vector) Fdb.get(0);
return v.get(c).getClass();}
public boolean isCellEditable(int row, int col)
{ return true; }
public void setValueAt(Vector value, int row, int col)
{
for(int i=0;i<value.size();i++)
{ for(int j=0;j<columnNames.size();j++) {
Fdb.setElementAt(value.get(j),j); }
}
fireTableCellUpdated(row, col);
}
}
問題は、コードを実行すると、テーブル GUI に初期値が表示されますが、2 つのコンボボックスで選択を変更して選択ボタンをクリックすると更新に失敗することです。ところで、選択ボタンは、アクション リスナーを実装するメソッドを呼び出します。
私を助けてください。私は Java のプロではありませんが、喜んで学びます。フォローアップの質問があれば、喜んで詳細をお知らせします。