【问题标题】:How can I limit the number of maximum selections for 1 selecting option of 20 JCombobox? javafor ex如何限制 20 JCombobox 的 1 个选择选项的最大选择数? javafor ex
【发布时间】:2017-09-17 14:41:13
【问题描述】:

我的 JComboBoxes 有 9 个(+ 默认值)可选选项(20 个都一样)。
这 20 个是 2 个 JComboBox 数组的一部分。 (每个 10-10 个)。 我想像这样限制它们:
如果从(例如)选项 4 中选择了 4 个并且用户选择了第 5 个
其中之一,则其中一个会跳回默认值以保持 4 的限制。

我该怎么做?
例如:

for (int i = 0; i < roles1.length; i++) {
            roles1[i] = new JComboBox();
            roles1[i].setModel(new DefaultComboBoxModel(new String[] { "Not Selected", "PartnerInCrime", "Driver",
                    "Gunman", "Coordinator", "Hacker", "Distraction", "GadgetGuy", "Bruglar", "ConMan" }));
            roles1[i].setBounds(boxPlace, 200, 100, 20);
            boxPlace += 105;
            getFrame().getContentPane().add(roles1[i]);

        }

【问题讨论】:

  • 能否添加一些代码?
  • 如您所愿:)

标签: java arrays jcombobox limiting


【解决方案1】:

如果我正确理解你的问题,我有一个想法,你可以从:

// create global HashMap that can records the occurrence of the selection of each item
Map<String, Integer> reference = new HashMap<String, Integer>(); 
// populate it
reference.put("PartnerInCrime", 0);
reference.put("Driver", 0);
reference.put("Gunman", 0);
reference.put("Coordinator", 0);
reference.put("Hacker", 0);
reference.put("Distraction", 0);
reference.put("GadgetGuy", 0);
reference.put("Bruglar", 0);
reference.put("ConMan", 0);

// then for every JComboBox in your array -> add action item listener to observe and control the selection like this


for(JComboBox<String> jcb : roles1){
    jcb.addItemListener(new ItemListener(){
      public void itemStateChanged(ItemEvent ie){
        if(ie.getStateChange() == ItemEvent.DESELECTED){ // decrement record
           if(!ie.getItem().toString().equals("Not Selected")){
             reference.put(ie.getItem().toString(), reference.get(ie.getItem().toString()) -1);
           }
        }
         else if(ie.getStateChange() == ItemEvent.SELECTED){
           if(!ie.getItem().toString().equals("Not Selected")){
             if(reference.get(ie.getItem().toString())>=4){ // if already selected 4 of them
                jcb.setSelectedIndex(0); // return to the default
              } 
              else{ // else record the selection 
                   reference.put(ie.getItem().toString(), reference.get(ie.getItem().toString()) +1);
              }    
          } 
        }
      }
   });
}

【讨论】:

    【解决方案2】:

    这里有一个建议可以引导你走向正确的方向。

    首先,您必须为每个 ComboBox 添加一个 ActionListener,它会调用一个方法,将所有选择与您当前的选择进行比较。

    roles1[i].addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
    
           // get the current selection
           JComboBox currentBox = (JComboBox)e.getSource();
           String currentSelection = (String)currentBox.getSelectedItem();
    
           // call the method and hand in your current selection
           checkForSelectionExceeding(currentSelection);            
    
        }
    });
    

    在您的扫描方法中,您应该在扫描时记住匹配的数量。如果超出限制,请将当前框重置为默认值并停止扫描。像这样的:

    private void checkForSelectionExceeding(String currentSelection){
       int matches = 0;
    
       for(int i=0; i<roles1.length; i++) {
          if(roles1[i].getSelectedItem().equals(currentSelection)) {
             matches++;
          }
    
          if(matches > 4) {
             roles1[i].setSelectedItem("Not Selected");
             break;
          }
       }
    }
    

    您只需稍微重构此解决方案即可按顺序扫描两个数组。

    希望这会有所帮助。

    【讨论】:

    • 谢谢,帮了大忙!
    • 在这种情况下,每次用户从任何组合框中进行选择时,它都必须遍历整个数组并查找每个选择的值。
    • 在约翰:我想说这两种方法都有其优点和缺点。由于需要循环,我的解决方案可能需要多几毫秒。您的建议需要额外的地图来保存参考信息。但如果只有 20 个 ComboBoxes,这些骗局都不是问题。 -- @Victor:如果您能标记或支持对您有帮助的解决方案,我将不胜感激。提前致谢。 :)
    • 刚刚完成!谢谢你的帮助!我真的很感激!
    猜你喜欢
    • 1970-01-01
    • 2012-08-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-01-04
    • 1970-01-01
    • 2016-04-18
    • 1970-01-01
    相关资源
    最近更新 更多