1

I want to create some swing component at runtime using for loop in JPanel, while it doesn't display anything. please help me.

Here is the code :

private String [] DTS = new String[]{"Number","Text","Date","Image","Document"};
private String [] CONS = new String[]{"Not Null", "Primary", "unique"};

private void GenerateButton_actionPerformed(ActionEvent e) {
    System.out.println(e.getActionCommand());
    System.out.println(TableName.getText());
    System.out.println(ColumnRange.getText());
    int Length = Integer.parseInt(ColumnRange.getText());
    JTextField [] FieldName = new JTextField[Length];
    JComboBox [] DataType = new JComboBox[Length];
    JComboBox [] Constraints = new JComboBox[Length];
    try {
        for (int i=0; i < Length; i++) {
            FieldName[i] = new JTextField();
            FieldName[i].setBounds(new Rectangle(35,150,200,20));
            DataType[i] = new JComboBox(DTS);
            DataType[i].setBounds(new Rectangle(35,150,200,20));
            Constraints[i] = new JComboBox(CONS);
            Constraints[i].setBounds(new Rectangle(35,150,200,20));
            this.add(FieldName[i],null);
            this.add(DataType[i], null);
            this.add(Constraints[i], null);
            this.validate();
        }
    } catch(Exception GE){
        System.out.println(GE);
    }
}
AlexR
  • 114,158
  • 16
  • 130
  • 208
Kernel
  • 165
  • 4
  • 14

1 Answers1

4
this.validate(); 
this.repaint(); 

And don't use setBounds(). Use LayoutManager instead. E.g. GridBagLayout

StanislavL
  • 56,971
  • 9
  • 68
  • 98