2012-03-17 11 views
1

私のユースケースは、List<String>Jpanelに渡され、Listの各Stringため、JPanelがUIコンポーネントを描画することです。このUIコンポーネントは3つのボタンで構成されています。私の使用例の現在のコードは次のとおりです。 -
「UIコンポーネント」のコードは、次の -JavaのSwingは - JPanelのとのPropertyChangeListener

public class MacroEditorEntity implements ActionListener { 
    private String macro; 
    private JButton upButton; 
    private JButton downButton; 
    private JButton MacroDetailsButton; 

    public MacroEditorEntity(String macro) { 
    this.macro = macro; 
    upButton = new JButton("Up"); 
    downButton = new JButton("Down"); 
    MacroDetailsButton = new JButton(macro); 

    upButton.addActionListener(this); 
    downButton.addActionListener(this); 
    MacroDetailsButton.addActionListener(this); 
} 

    @Override 
    public void actionPerformed(ActionEvent evt) { 

     if(evt.getSource().equals(MacroDetailsButton)) 
     { 
      System.out.println(macro); 
     } 
    } 

    public JButton GetUpButton() 
    { 
     return upButton; 
    } 
    public JButton GetDownButton() 
    { 
     return downButton; 
    } 
    public JButton getMacroDetailsButton() 
    { 
     return MacroDetailsButton; 
    } 
} 

私のパネルのためのコードは次のようである -

public class MacroEditor extends JPanel implements PropertyChangeListener { 

    private static final long serialVersionUID = 1L; 
    private List<String> stringlist; 

    public MacroEditor(List<String> list) { 

     this.stringlist = list; 
     setupComponents(); 
     validate(); 
     setVisible(true); 
    } 

    public void setupComponents() 
    { 
     Box allButtons = Box.createVerticalBox(); 
     for(String string : stringlist) 
     { 
      MacroEditorEntity entry = new MacroEditorEntity(string); 
      Box entryBox = Box.createHorizontalBox(); 
      entryBox.add(entry.GetUpButton()); 
      entryBox.add(Box.createHorizontalStrut(15)); 
      entryBox.add(entry.getMacroDetailsButton()); 
      entryBox.add(Box.createHorizontalStrut(15)); 
      entryBox.add(entry.GetDownButton()); 

      allButtons.add(entryBox); 
     } 

     add(allButtons); 
    } 

    @Override 
    public void propertyChange(PropertyChangeEvent arg0) { 
     revalidate(); 
     repaint(); 
    } 

} 

コードが渡されList内のすべてのStringsのために正常に動作します。私は、追加または削除のようなListに起こりうる変更をピックアップし、それに応じて対応する対応するUIコンポーネントを追加/削除したいと考えています。私はこれがPropertyChangeListenerを使用して行うことができると思うが、私のコードでそれを説明することができませんでした。
Listが変更されるとすぐにパネルレンダリング/再レンダリングを行う方法に関するアイデアや提案は役に立ちます。

+0

Javaの命名規則を学んでください。 – kleopatra

答えて

2

ここに必要なのは、観察可能なコレクションです。これはそれを行う必要があります:http://commons.apache.org/dormant/events/apidocs/org/apache/commons/events/observable/ObservableCollection.html

編集:あなたのGUIにグレードってこんなモンオブジェクトを結合して、(修正に反応するのに役立ちます別の概念:ところで

public class ObservableListExample implements StandardPostModificationListener, 
    StandardPreModificationListener { 

    public static void main(String[] args) { 
     new ObservableListExample(); 
    } 

    public ObservableListExample() { 

     ObservableList list = ObservableList.decorate(new ArrayList<>(), 
       new StandardModificationHandler()); 

     list.getHandler().addPostModificationListener(this); 
     list.getHandler().addPreModificationListener(this); 
     //.... 

    } 

    @Override 
    public void modificationOccurring(StandardPreModificationEvent event) { 
     // before modification 
     Collection changeCollection = event.getChangeCollection(); 
     if (event.isTypeAdd()) { 
      // changeCollection contains added elements 
     } else if (event.isTypeReduce()) { 
      // changeCollection contains removed elements 
     } 
    } 

    @Override 
    public void modificationOccurred(StandardPostModificationEvent event) { 
     // after modification 
     Collection changeCollection = event.getChangeCollection(); 
     if (event.isTypeAdd()) { 
      // changeCollection contains added elements 
     } else if (event.isTypeReduce()) { 
      // changeCollection contains removed elements 
     } 
    } 
} 

ここでのコードは、あなたが要求したスニペットです双方向に)データバインディングです。スイングでよく使用されるデータバインディングライブラリthisを見てください。

+1

が完璧です。私はこのクラスが存在していることを認識せず、答えに書いていました。ありがとう! – vextorspace

+0

http://commons.apache.org/dormant/index.html – Simon

+0

@Simon - あなたの答えをありがとう。私はjavadocを使い、Observerのパターンをよく理解しています。 ObservableCollectionが私のコードにどのように結びついているかは完全にはわかりません。私はPropertyChangeListenerを必要としませんか? ObservableCollectionを使用する方法のコードスニペットと、それが自分のコードにどのように結びついているかは、わかりやすく理解するのに役立ちます。 – ping

関連する問題