InterCity
InterCity

Reputation: 102

Displaying swing components with a loop

I was tasked with making a database application. I'm coming to Java from PHP, so I was used to making a html table and displaying rows again and again with foreach(). My approach doesn't seem to work - the fields don't display:

    JTextField[] textFields = new JTextField[3];

    textFields[1] = new JTextField();
    GridBagConstraints gbc_textField = new GridBagConstraints();
    gbc_textField.fill = GridBagConstraints.HORIZONTAL;
    gbc_textField.insets = new Insets(0, 0, 0, 5);
    gbc_textField.gridx = 0;
    gbc_textField.gridy = 0;
    panel_1.add(textFields[1], gbc_textField);
    textField.setColumns(10);

How can I make such a thing as a loop-displayed table of text fields as I know it from PHP in Java?

Upvotes: 0

Views: 117

Answers (2)

GlacialMan
GlacialMan

Reputation: 597

I don't know PHP and i don't know what you want. But with for loop maybe you can do that.

Like:

JTextField[] textFields = new JTextField[3];

for(int i;i<textFields.lenghts;i++){

  textFields[i] = new JTextField();
  GridBagConstraints gbc_textField = new GridBagConstraints();
  gbc_textField.fill = GridBagConstraints.HORIZONTAL;
  gbc_textField.insets = new Insets(0, 0, 0, 5);
  gbc_textField.gridx = 0;
  gbc_textField.gridy = 0;
  textField.setColumns(10);
  panel_1.add(textFields[i], gbc_textField);

}

If you want Table then the better is to use JTabel.

Upvotes: 0

yamounane
yamounane

Reputation: 71

Have you tried to use JTable() component ? See JTable documentation

Upvotes: 3

Related Questions