2

I have a form to add a text box to GridBagLayout. Is it possible that each addition of a text field, the field will have a different name for the variable? Because now every time adds the same name, but different x, y I was thinking to create a static field as a counter to be added to the variable name, but I do not know how to add this number to the name.

Here is my code:

    JTextArea dodawanyTextField = new javax.swing.JTextArea();


    dodawanyTextField.setText(nazwaLekcjiTextField.getText());

    gridBagConstraints = new java.awt.GridBagConstraints();
    gridBagConstraints.weighty = 1;
    gridBagConstraints.fill = GridBagConstraints.VERTICAL;
    //gridBagConstraints.gridheight = 100;
    gridBagConstraints.gridheight = ileGodzinLekcji;
    gridBagConstraints.gridx = dzienLekcji;
    gridBagConstraints.gridy = godzinaLekcji;


    if (wykladRadioButton.isSelected()) {
        dodawanyTextField.setBackground(new java.awt.Color(245, 184, 0));
    } else if (cwiczeniaRadioButton.isSelected()) {
        dodawanyTextField.setBackground(new java.awt.Color(61, 245, 0));
    } else if (laboratoriumRadioButton.isSelected()) {
        dodawanyTextField.setBackground(new java.awt.Color(0, 184, 245));
    }
    dodawanyTextField.setMaximumSize(new java.awt.Dimension(125, 800));
    dodawanyTextField.setPreferredSize(new java.awt.Dimension(125, 50));
    dodawanyTextField.setWrapStyleWord(true);
    dodawanyTextField.setLineWrap(true);
    jPanel3.add(dodawanyTextField, gridBagConstraints);
Aclber
  • 41
  • 1
  • 1
  • 6
  • 2
    No, that's not possible. A variable can't have a dynamic name. What are you trying to achieve? If you want to keep a reference to all the added text areas, add them into a `List`. – JB Nizet Jun 08 '13 at 08:02
  • Then I want to add the GridBagLayout to the database. The problem arises later as I wanted to edit some text field, because I will not be able to tell what is the name of the element. I think to add to the database fields such as "variable name", "text", "x", "y", "type". – Aclber Jun 08 '13 at 08:05
  • I am writing an applet "schedule", so maybe instead of używaniaGridBagLayout, I use a JTable? What do you think, whether it is the right light for this type of program? – Aclber Jun 08 '13 at 08:38
  • 2
    Whether you use a table or something other for the UI shouldn't have any impact on the model of the data you're going to save and load. And Java variable names have absolutely nothing to do with data. – JB Nizet Jun 08 '13 at 08:40

1 Answers1

0

This is likely the wrong solution to you particular problem, but to directly answer the question about names, you can give a name to any awt Component:

dodawanyTextField.setName("useful identifier");

and retrieve it later with getName(). As said it's probably not what you should use, since you want to to attach more data than just an identifier to the text field. Anyway it's a trick that can sometimes be used when the simplicity of the problem does not warrant a complicated solution.

kiheru
  • 6,588
  • 25
  • 31