【问题标题】:Applying a Counter to Item in List将计数器应用于列表中的项目
【发布时间】:2011-07-21 19:27:15
【问题描述】:

我有一个通过本地文本文件填充的列表。我有以下代码,可以在单击按钮时简单地打印所选项目。

private void jButton5ActionPerformed(java.awt.event.ActionEvent evt) {
    int[] selection = jList3.getSelectedIndices();
            //  selection.toString();
            for (int i = 0; i < selection.length; i++){
                Object selString = jList3.getModel().getElementAt(selection[i]);

                System.out.println(selString);

            }
    }

我希望每个按钮单击每个对象以某种方式记录下来,而不是打印项目。我不知道要实现什么样的组件、方法等。任何指导表示赞赏。

我的最终结果将与此类似。

System.out.println(SelString has been clicked X amount of times);

【问题讨论】:

标签: java swing netbeans counter


【解决方案1】:

使用散列映射,将对象 (selString) 作为键,将计数器作为值。比如:

private Map<Object, Integer> buttonMap = new HashMap<Object, Integer>

private void jButton5ActionPerformed(java.awt.event.ActionEvent evt) {
    Integer counter = null;
    int[] selection = jList3.getSelectedIndices();

    for (int i = 0; i < selection.length; i++){ 
        Object selString = jList3.getModel().getElementAt(selection[i]);
        counter = buttonMap.get(selString);
        if(counter == null) {
            buttonMap.put(selString, new Integer(0));
        }

        buttonMap.put(selString, new Integer(counter.intValue() + 1));
        System.out.println(selString + " has been clicked " + buttonMap.get(selString) + " times.");
    }
}

【讨论】:

  • 感谢您的回答,我收到了一些空异常等,因此我修改了您的代码。我选择了 if/else 语句。
【解决方案2】:

您可以使用 PropertyChangeSupport 来通知每次点击 jList 项目,此外您应该创建一个侦听器来接收事件通知(通过propertyChangeSupport.addPropertyChangeListener)。

在那里,您可以获得事件属性,例如属性名称和属性的新值,在这种情况下,这些属性将在 jList3 上被选中,为了计算某个项目被点击的次数,您可以使用 HashMap,设置key 作为 jList 的项目索引和关联的值,该项目被点击了多少次:

PropertyChangeSupport propertyChangeSupport = new PropertyChangeSupport(this);
HashMap<Integer, Integer> clickCounter = new HashMap<Integer, Integer>();

    public NewJFrame() {
        initComponents();
        propertyChangeSupport.addPropertyChangeListener(new PropertyChangeListener() {

            public void propertyChange(PropertyChangeEvent evt) {
                if (evt.getPropertyName().equals("selectedIndex")) {
                    System.out.println("Selected index: " + evt.getNewValue());
                    System.out.println("Selected text: " + jList3.getModel().getElementAt(evt.getNewValue()));
                    if (clickCounter.containsKey((Integer) evt.getNewValue())) {
                        clickCounter.put((Integer) evt.getNewValue(), clickCounter.get((Integer) evt.getNewValue()) + 1);
                    } else {
                        clickCounter.put((Integer) evt.getNewValue(), 1);
                    }
                }
            }
        });
    }

    private void jList1MouseClicked(java.awt.event.MouseEvent evt) {
        // TODO add your handling code here:
        propertyChangeSupport.firePropertyChange("selectedIndex", -1, jList3.getSelectedIndex());
    }

您可以随时检索某些项目在访问 clickCounter 时被点击了多少次

【讨论】:

    【解决方案3】:

    我建议使用一个内部类来保存您当前放入 JList 的任何对象,并添加一个计数器成员变量以及覆盖 toString()。

    class MyListItem 
    {
       int selectionCount;
       Object listItem; //may consider generics here, but left them out cause they can be tricky
       public MyListItem(Object item) 
       {
          selectionCount=0;
          listItem=item;
       }
       public void incrementSelectionCount()
       {
          selectionCount++;
       }
       public String toString()
       {
          return listItem.toString() + " has been clicked " + selectionCount + " times."); 
       }
    }
    

    然后在你的动作监听器中

    private void jButton5ActionPerformed(java.awt.event.ActionEvent evt) 
    {                   
        int[] selection = jList3.getSelectedIndices();                           
    
        for (int selectedIndex : selection)
        {
           Object selString = jList3.getModel().getElementAt(selectedIndex);
           if(selString instanceOf MyListItem)
           {
               MyListItem selItem = (MyListItem) selString;
               selItem.incrementSelectionCount();
               System.out.println(selString);  
           }                                        
        }                   
    } 
    

    这应该可以节省查找、装箱等方面的时间。此外,这有助于随着项目的发展而保持理智,因为 MyListItem 可以发展为处理您将来可能需要的所有类型的操作,以防您想要不同的东西发生在按钮按下以外的事情上。这里的基本思想是 MyListItem 应该跟踪您有兴趣跟踪的所有内容,因此您不需要多个列表,更糟糕的是,要记住将项目添加到 JList 和 HashMap 或任何其他数据结构中。这样,它要么存在于它需要存在的每个数据结构上,要么根本不存在。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-08-23
      • 1970-01-01
      • 2019-08-28
      • 1970-01-01
      • 2013-03-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多