帮助实现 JComboBox[] Listener

发布于 2024-11-10 06:49:47 字数 962 浏览 5 评论 0原文

1) 在下面的方法(actionListener)中,用户从 JComboBox 中选择一个等级(例如 AF)。

2) 有多个 JComboBox,每个选择都存储到单个 String[] 数组中。

问题: 这里存在一个困境,如果用户返回并更改从随机 JComboBox 中所做的选择,则先前的等级选择不会在数组中被替换,但所做的新选择将存储在下一个数组索引中。

我怎样才能让程序取代以前的年级选择,而不仅仅是添加新的选择?

相关变量:

int counter;
private JComboBox[] gradeField;
//grade.userGrades[] is array of grades taken from selected combo boxes                  

Action Listener 匿名类:

gradeField[counter].addActionListener(new ActionListener () {
            @Override
            public void actionPerformed(ActionEvent e) {
                Object holder = e.getSource();
                JComboBox tempGradeBox = (JComboBox)holder;
                String theGrade = (String)tempGradeBox.getSelectedItem();
                grade.userGrades[grade.getNext()] = theGrade;
                grade.updateNext();
            }                       
        });

提前感谢您的帮助。

1) In the following method (actionListener) a user select a grade (e.g. A-F) from a JComboBox.

2) There are multiple JComboBoxes, and each selection made gets stored into a single String[] array.

PROBLEM:
Here is the dilemma, if a user goes back and changes a selection made from a random JComboBox the previous grade selection does not get replaced in the array, however the new selection made gets stored at the next array index.

How can I make the program replace the previous grade selection and not just add the new selection?

relevant variables:

int counter;
private JComboBox[] gradeField;
//grade.userGrades[] is array of grades taken from selected combo boxes                  

Action Listener anonymous class:

gradeField[counter].addActionListener(new ActionListener () {
            @Override
            public void actionPerformed(ActionEvent e) {
                Object holder = e.getSource();
                JComboBox tempGradeBox = (JComboBox)holder;
                String theGrade = (String)tempGradeBox.getSelectedItem();
                grade.userGrades[grade.getNext()] = theGrade;
                grade.updateNext();
            }                       
        });

Thanks in advance for any help.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

救赎№ 2024-11-17 06:49:47

我将成绩保存在数组中并递增索引,

那么您不应该增加索引。这假设用户按顺序从组合框中选择成绩。正如您所发现的,用户通常可以随机工作。

相反,您需要知道哪个组合框已更改,然后更新数组中的相应条目。

或者不同的解决方案可能是最后更新您的数组。所以也许您有一个“处理结果”按钮。然后您可以依次循环所有组合框以获取所选值。

I save the grade in an array and increment the index,

Well you should not be incrementing the index. This assumes that the user selects the grades from the combo box in a sequential order. As you have discovered users can often work randomly.

Instead you need to know which combo box has been changed and then update the appropriate entry in your array.

Or a different solution might be to update your array at the end. So maybe you have a "Process Results" button. Then you can sequentually loop through all the combo boxes to get the selected value.

り繁华旳梦境 2024-11-17 06:49:47

更新与组合框具有相同索引的用户等级:

    final int index = counter;
    gradeField[counter].addActionListener(new ActionListener () {
        @Override
        public void actionPerformed(ActionEvent e) {
            Object holder = e.getSource();
            JComboBox tempGradeBox = (JComboBox)holder;
            String theGrade = (String)tempGradeBox.getSelectedItem();
            grade.userGrades[index] = theGrade;
        }                       
    });

Update the user grade being at the same index as the combo box:

    final int index = counter;
    gradeField[counter].addActionListener(new ActionListener () {
        @Override
        public void actionPerformed(ActionEvent e) {
            Object holder = e.getSource();
            JComboBox tempGradeBox = (JComboBox)holder;
            String theGrade = (String)tempGradeBox.getSelectedItem();
            grade.userGrades[index] = theGrade;
        }                       
    });
桃扇骨 2024-11-17 06:49:47

这是 JB Nizet 答案的另一个变体:

class OuterClass
{
 ...

 gradeField[counter].addActionListener( new GradeSettingActionListener( counter ) );

 ...
 class GradeSettingActionListener implements ActionListener
 {
  // -- Doesn't have to be final here (it does in JB's answer), but I like to be restrictive.
  private final int index;

  public GradeSettingActionListener( int index )
  {
   this.index = index;
  }

  @Override
  public void actionPerformed( ActionEvent e )
  {
   Object holder = e.getSource();
   JComboBox tempGradeBox = (JComboBox) holder;
   String theGrade = (String) tempGradeBox.getSelectedItem();
   grade.userGrades[index] = theGrade;
  }
 }
}

这种方法通过添加内部类来删除匿名类。内部类仍然可以访问grade。除非您稍后有机会分裂内部类,否则您在这里不会获得太多收益。

当然,camickr关于一次处理所有成绩的建议也可能是有效的,这取决于其他要求(即,在成绩存储在数组中之后是否进行额外的处理,这似乎是可能的)。

Here's another variation of JB Nizet's answer:

class OuterClass
{
 ...

 gradeField[counter].addActionListener( new GradeSettingActionListener( counter ) );

 ...
 class GradeSettingActionListener implements ActionListener
 {
  // -- Doesn't have to be final here (it does in JB's answer), but I like to be restrictive.
  private final int index;

  public GradeSettingActionListener( int index )
  {
   this.index = index;
  }

  @Override
  public void actionPerformed( ActionEvent e )
  {
   Object holder = e.getSource();
   JComboBox tempGradeBox = (JComboBox) holder;
   String theGrade = (String) tempGradeBox.getSelectedItem();
   grade.userGrades[index] = theGrade;
  }
 }
}

This approach removes the anonymous class by adding an inner class. The inner class will still have access to grade. You don't gain much here unless there's a chance you'll be splitting out the inner class later.

Of course, camickr's suggestion to process all the grades at once may also be valid, depending on other requirements (i.e., whether additional processing is done after the grades are stored in the array, which seems likely).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文