将计数器应用于列表中的项目

时间:2011-07-21 19:27:16

标签: java swing netbeans counter

我有一个通过本地文本文件包含的列表。我有以下代码,只需单击按钮即可打印所选项目。

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

3 个答案:

答案 0 :(得分:3)

将散列图与对象(设置字符串)一起用作键,将计数器用作值。类似的东西:

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

答案 1 :(得分:3)

每次点击jList项时都可以使用PropertyChangeSupport通知,除此之外你应该创建一个监听器来接收事件通知(通过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());
    }

您可以随时查看点击certian项目的次数,访问clickCounter

答案 2 :(得分:2)

我建议使用一个内部类,它保存您当前放入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或任何其他数据结构。这样它就可以在它需要的每个数据结构上完成。或者根本不需要。