2

変数「priority」を持つTrainingClassオブジェクトのArrayListがあります。

設定フレームを作成しています。現在ArrayListにある要素ごとに、ユーザーが優先順位を設定するTextFieldを作成しています。

これが生成される方法です

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);
 }

次に、変更リスナーを追加しますが...

どのフィールドが変更されたかがわかったら、ArrayList mTrainingClassesの適切な要素にアクセスするにはどうすればよいですか?

たとえば、phpでは、次のように単純に作成します。

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

しかし、私が理解している限り、Javaではこれを行うことはできません。それで、私はどのように進めるべきですか?

各要素のフィールド名とリスナーを手動で設定する必要がありますか?他にも解決策があると思いますが、現時点ではわかりません。

(私は、次のようなフィールドにもArrayListを使用できることを知っています

txtPriority.add(new JTextField(3));

しかし、この場合、変更されたフィールドに対応するインデックスをどのように知ることができますか?)。

4

4 に答える 4

4

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.

于 2012-11-22T07:30:52.680 に答える
4

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
于 2012-11-22T07:31:17.247 に答える
3

In your loop, you can populate a Map<JTextField, TrainingClass>. Then you can use that to look up the element from the changed field.

Map<JTextField, TrainingClass> fieldMap = new HashMap<>();
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);
    map.put(txtPriority, tclass);
}

Alternatively, you can subclass JTextField and declare a data field that you can then reference directly in event handling.

于 2012-11-22T07:26:07.767 に答える
3

You need some kind of mapping between the JTextField and the TrainingClass. Either make text field a property of your class or make a map that maps the two.

Map<TrainingClass, JTextField> myMap= new HashMap<TrainingClass, JTextField>();

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);

    // map the textField to the training class
    myMap.put(txtPriority, tclass);
}

When the field changes inside the listener method, you'd simply call:

public void eventListenerMethod(InputEvent e) {
     JTextField fieldThatGeneratedEvent= e.getSource();
     TrainingClass tClass= myMap.get(fieldThatGeneratedEvent);
}
于 2012-11-22T07:50:24.793 に答える