I want to create a dynamic table based on number of columns and datatype. The problem is that when I want to get element from JTextField
and JComboBox
generic type list. I have to iterate from different generic type, but adding into one generic type such Table type. I think the main problem is in the cretaeTable()
method.
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());
}
}