2012-03-17 2 views
1

내 유스 케이스는 JPanel는 UI 구성 요소를 렌더링하는 List<String>Jpanel 및 상기 ListString 위해 전달된다는 것이다. 이 UI 구성 요소는 3 개의 버튼으로 구성되며 현재 사용중인 코드는 다음과 같습니다. -
'UI 구성 요소'에 대한 코드는 다음 -자바 스윙 - 인 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

자바 이름 지정 규칙을 배우고 – 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

@ 시몬 - 답변 해 주셔서 감사합니다. 나는 javadoc을 살펴보고 Observer 패턴을 잘 이해하고있다. ObservableCollection이 어떻게 내 코드에 묶일 지 모르겠습니다. 그럼 PropertyChangeListener가 필요하지 않을까요? ObservableCollection을 사용하는 방법에 대한 코드 스 니펫 (snippet)과 내 코드에 묶는 방법을 이해하면 도움이됩니다. – ping