0

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());
            }
        }
mKorbel
  • 109,525
  • 20
  • 134
  • 319
Mohammod Hossain
  • 4,134
  • 2
  • 26
  • 37
  • everything in answer by (@trashgod) were correct, your model is wrong, move whatever about model methods from Table to model, simple remove that, don't put JComponent!!! to the XxxTableModel, there could be stored only Sting value, read Oracle tutotrial about Renderer and Editor Concept, your question hasn't something about generic, just about Renderer and Editor Concept... – mKorbel Nov 18 '12 at 07:55
  • i want to create table script such table name, column name, data type ,length from value JAVA Swing UI textinput,combo. and user can input table name and number of column field. when click button on create it add all column name,datatype,length and generate and ddl script for creating table in the specified database – Mohammod Hossain Nov 18 '12 at 09:15

0 Answers0