我想根据列数和数据类型创建一个动态表。问题是当我想从泛型类型列表JTextField
中获取元素时。JComboBox
我必须从不同的泛型类型进行迭代,但要添加到一种泛型类型中,例如 Table 类型。我认为主要问题在于cretaeTable()
方法。
public class Table {
public String getColumnName() {
return columnName;
}
public void setColumnName(String columnName) {
this.columnName = columnName;
}
public String getDatatype() {
return datatype;
}
public void setDatatype(String datatype) {
this.datatype = datatype;
}
private String columnName;
private String datatype;
}
List<JTextField> fields = new ArrayList<JTextField>();
List<JComboBox> combos = new ArrayList<JComboBox>();
// adding elemet
public void addNewElements(ActionEvent evt) {
System.out.println("txtInputColNum.getText():: " +txtNumColumn.getText());
if(!(txtNumColumn.getText()== null || txtNumColumn.getText().equals(""))) {
if(Integer.parseInt(txtNumColumn.getText())>0){
noOfColumn =Integer.parseInt(txtNumColumn.getText());
int txtFieldX= 10;
int comboFieldX = 120;
for (int i= 0; i < noOfColumn; i++){
JTextField newJTextField = new JTextField("",4);
newJTextField.setBounds(txtFieldX, 60, 100, 25);
String course[] = {"VARCHAR2","NUMBER","DATE"};
JComboBox combo = new JComboBox(course);
combo.setBounds(comboFieldX, 60, 100, 25);
fields.add(newJTextField);
this.add(newJTextField);
this.add(combo);
combos.add(combo);
txtFieldX = txtFieldX+250;
comboFieldX = comboFieldX+250;
}
}
}
this.repaint();
this.validate();
}
// call createTable method
public void createTable(ActionEvent evt) {
List<Table> tabList = new ArrayList<Table>();
System.out.println("fields "+ fields.size());
for(JTextField textField :fields){
Table table = new Table();
//System.out.println("textField:: "+ textField.getText());
table.setColumnName(textField.getText().toString());
tabList.add(table);
}
for(JComboBox comboField :combos){
Table table2 = new Table();
System.out.println("comboField:: "+ comboField.getSelectedItem().toString());
table2.setDatatype(comboField.getSelectedItem().toString());
tabList.add(table2);
}
for(Table tableModel:tabList){
System.out.println("getColumnName "+ tableModel.getColumnName());
System.out.println("getDatatype "+ tableModel.getDatatype());
}
}