我正在从“步骤”列表中填充JTable动态。通过包含多个动作的可能性来排序从其他步骤“打开”的步骤,而所有其他步骤类型仅包含一个动作。因此,我希望 - 当达到'打开'类型的步骤时 - 将所有动作添加到同一个表格中,如下所示:
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 .. ...
其中'delete'和'replace'是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;
}
};
}
和Step class是:
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
anyOne有什么想法吗?
提前感谢!
答案 0 :(得分:0)
我解决问题的方法是改变Step类stracture以包含一个uniq字符串中的整个步骤数据,然后我只需填写'oppening'步骤数据,就像所有其他步骤类型一样,没有内部环。我知道它可能是解决它的更好方法,但由于其他一些原因,我需要改变Step类结构,所以我利用它...