我正在从“步骤”列表中动态填充 JTable。'opening'排序步骤不同于其他步骤,因为它可以包含多个动作,而所有其他步骤类型仅包含一个动作。因此,我希望 - 当达到“开放”类型的步骤时 - 将其所有操作都添加到同一个表中,如下所示:
step name Action on Object value result
opening full open door1 30 ... delete replace
haulgh open door2 40 delete replace
wholeOpen door1 10 delete replace
comparison compare state1 .. ...
其中“删除”和“替换”是 JButton 的扩展。我写的代码如下:
public DefaultTableModel ListToTableModel(Object[] l, String tableName) throws Exception {
Vector<String> columnNames = null;
Vector<Vector<Object>> data = new Vector<>();
columnNames = new Vector<>(Arrays.asList(" Step name: "," Action: "," On object: "," Action value: "," Action result: "," "," "));
for (int i = 0; i < l.length; i++) {
for(int j=0;j<((Step) l[i]).action.size();j++){
Vector<Object> vector = new Vector<>();
String string="";int k=0;
if(((Step) l[i]).Name=="opening"){
vector.add(((Step) l[i]).Name);
for(k=0;k<((Step) l[i]).action.size();k++){
string+=((Step) l[i]).action.get(k)+"\n";
}
vector.add(string);
string="";
for( k=0;k<((Step) l[i]).onObject.size();k++){
string+=((Step) l[i]).onObject.get(k)+"\n";
}
vector.add(string);
string="";
for(k=0;k<((Step) l[i]).value.size();k++){
string+=((Step) l[i]).value.get(k)+"\n";
}
vector.add(string);
string="";
for( k=0;k<((Step) l[i]).result.size();k++){
string+=((Step) l[i]).result.get(k)+"\n";
}
vector.add(string);
break;
}
else
{
vector.add(((Step) l[i]).Name);
vector.add(((Step) l[i]).action.get(j));
vector.add(((Step) l[i]).onObject.get(j));
vector.add(((Step) l[i]).value.get(j));
vector.add(((Step) l[i]).result.get(j));
vector.add("delete");
vector.add("Replace");
}
data.add(vector);
}
}
return new DefaultTableModel(data, columnNames) {
@Override
public boolean isCellEditable(int rowIndex, int mColIndex) {
return true;
}
};
}
和步骤类是:
public class Step {
public String Name=null;
public List<String> action=null;
public List<String> onObject=null;
public List<String> value=null;
public List<String> result=null;
public Step(String n){
Name=n;
action=new ArrayList<String>();
onObject=new ArrayList<String>();
value=new ArrayList<String>();
result=new ArrayList<String>();
}
public void add(String act,String onobject ,String val,String res){
action.add(act);
onObject.add(onobject);
value.add(val);
result.add(res);
}
但我打电话时得到的唯一结果:
Step step=new Step("opening");
step.add("full open","door1","30.0","door_1");
step.add("haulgh open","door2","40.0","door_2");
step.add("whole open","door3","40.0","door_3");
Controller.getStepList().add(step);
step=new Step("comparison");
step.add("compare","state1","--","state_1");
Controller.getStepList().add(step);
是:
step name Action on Object value result
comparison compare state1 -- state_1
有人知道它有什么问题吗?
预先感谢!