2011-07-21 3 views
0

私は、ローカルテキストファイルを介して汚染されたリストを持っています。ボタンがクリックされたときに、選択した項目を簡単に印刷する次のコードがあります。リスト内のアイテムにカウンタを適用する

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

またはJButton – mKorbel

答えて

3

オブジェクト(selString)をキーとして使用し、値としてカウンタを使用します。あなたは(propertyChangeSupport.addPropertyChangeListenerを通じて)イベント通知を受信するためのリスナーを作成する必要があります以外に、JListの項目をクリックされるたびに通知するためにによってPropertyChangeSupportを使用することができます

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

の代わりにJPopupMenu http://download.oracle.com/javase/tutorial/uiswing/components/menu.html#popup私はいくつかのnull例外などを取得していたので、私はあなたのコードを変更しました。私はif/elseステートメントを使いました。 – sealz

3

:のようなもの。

これで、プロパティ名やプロパティの新しい値などのイベントプロパティを取得することができます。この場合、jList3の項目が選択されます。この場合、いくつかの項目がクリックされた回数をカウントしたり、HashMapを使用したり、

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

にアクセスクリックされた回数をretriveことができます任意の時点で:アイテムがクリックされたどのように多くの時間をJListの項目のインデックスと関連付けられた値としてキー現在JListに入れているオブジェクトを保持する内部クラスを使用して、 toString()をオーバーライドします。 MyListItemに対処するために成長させることができるので、プロジェクトの成長に合わせて、あなたのアクションリスナーで次に

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は、JListとHashMapまたは他のデータ構造の両方に項目を追加することを忘れないように、複数のリストを必要としないように、追跡に興味のあるものすべてを追跡する必要があるということです。このようにして、すべてのデータ構造上にあるかどうかを判断する必要があります。

関連する問題