Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java: change a variable depending on dynamically generated text field name?

I have an ArrayList of TrainingClass objects with a variable "priority".

I am making a settings frame, where for each element currently in the ArrayList I make a TextField where the user sets priority.

This is how it is generated

for (TrainingClass tclass : mTrainingClasses) {
                  //Loop the ArrayList
        JTextField txtPriority = new JTextField(3);
        txtPriority.setBounds(10,10,100,20);
        txtPriority.setText("" + tclass.getPriority());
        getContentPane().add(txtPriority);
 }

Now I would add a change listener, but...

Once I know which field has been changed, how can I access the proper element of the ArrayList mTrainingClasses?

In php, for example, I would simply make something like:

 $mTrainingClasses->$changed_field->setPriority($new_value);

But, as far as I understand, I can’t do this in Java. So, how should I proceed?

Do I need to manually set the field name and listener for each element? I’m sure there is some other solution, but I have no idea at this point.

(I know I could use an ArrayList for the fields as well, such as

txtPriority.add(new JTextField(3));

But in this case, how do I know which index corresponds to the field that has been changed? )

like image 780
DavidTonarini Avatar asked Mar 31 '26 15:03

DavidTonarini


2 Answers

Have a list of Text Fields

List<JTextField> textFields = new ArrayList<JTextField>();

Change the loop like the following where you add all text fields to above list

for (TrainingClass tclass : mTrainingClasses) {
        //Loop the ArrayList
        JTextField txtPriority = new JTextField(3);
        txtPriority.setBounds(10,10,100,20);
        txtPriority.setText("" + tclass.getPriority());
        getContentPane().add(txtPriority);
        textFields.add(txtPriority);
}

In your listener you can do the following

mTrainingClasses.get(textFields.indexOf((JtextField) event.getSource()));

The above will return the TrainingClass which got changed.

like image 187
shazin Avatar answered Apr 02 '26 06:04

shazin


There are several options:

  • Pass the TrainingClass element to the listener which you attach to the textfield. This will require to attach the listener in your for loop where you have access to both the TrainingClass and JTextField variable
  • Use a Map as suggested by @Ted Hopp
  • Use a List as you already suggested. Trick is to store an index in the JTextField so that afterwards you know which JTextField corresponds to which element in the List. You can use JComponent#putClientProperty and JComponent#getClientProperty for this.
  • You can use those JComponent#putClientProperty and JComponent#getClientProperty methods to store the TrainingClass variable directly
like image 28
Robin Avatar answered Apr 02 '26 04:04

Robin



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!