【问题标题】:Java: change a variable depending on dynamically generated text field name?Java:根据动态生成的文本字段名称更改变量?
【发布时间】:2012-11-10 13:12:17
【问题描述】:

我有一个包含变量“优先级”的 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));

但是在这种情况下,我怎么知道哪个索引对应于已更改的字段? )

【问题讨论】:

    标签: java swing variables dynamic jtextfield


    【解决方案1】:

    在您的循环中,您可以填充Map<JTextField, TrainingClass>。然后您可以使用它从更改的字段中查找元素。

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

    或者,您可以继承 JTextField 并声明一个数据字段,然后您可以在事件处理中直接引用该字段。

    【讨论】:

      【解决方案2】:

      有一个文本字段列表

      List<JTextField> textFields = new ArrayList<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);
              textFields.add(txtPriority);
      }
      

      在您的听众中,您可以执行以下操作

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

      以上将返回已更改的 TrainingClass。

      【讨论】:

        【解决方案3】:

        有几种选择:

        • TrainingClass 元素传递给附加到文本字段的侦听器。这将需要在您可以访问 TrainingClassJTextField 变量的 for 循环中附加侦听器
        • 按照@Ted Hopp 的建议使用Map
        • 按照您的建议使用List。诀窍是在JTextField 中存储一个索引,以便之后您知道哪个JTextField 对应于List 中的哪个元素。为此,您可以使用 JComponent#putClientPropertyJComponent#getClientProperty
        • 您可以使用JComponent#putClientPropertyJComponent#getClientProperty 方法直接存储TrainingClass 变量

        【讨论】:

          【解决方案4】:

          您需要在 JTextField 和 TrainingClass 之间建立某种映射。要么让文本字段成为你的类的属性,要么制作一个映射这两者的地图。

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

          当侦听器方法中的字段发生变化时,您只需调用:

          public void eventListenerMethod(InputEvent e) {
               JTextField fieldThatGeneratedEvent= e.getSource();
               TrainingClass tClass= myMap.get(fieldThatGeneratedEvent);
          }
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 2020-07-07
            • 1970-01-01
            • 1970-01-01
            • 2023-03-04
            • 2012-10-28
            • 1970-01-01
            • 2013-06-22
            相关资源
            最近更新 更多