我的用例是将一个List<String>传递给一个Jpanel,对于List中的每个String,JPanel都会呈现一个UI组件。这个UI组件由3个按钮组成,我当前针对给定用例的代码如下所示。--
“UI组件”的代码如下--
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有变化,都会很有帮助。
发布于 2012-03-18 04:53:32
这里您需要的是一个可观察的集合。这应该可以做到:http://commons.apache.org/dormant/events/apidocs/org/apache/commons/events/observable/ObservableCollection.html
编辑:
以下是您请求的代码片段:
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
}
}
}顺便说一句:另一个有助于将业务对象绑定到GUI并对修改做出反应(双向)的概念是数据绑定。看一看this,这是一个通常与Swing一起使用的数据绑定库。
https://stackoverflow.com/questions/9753605
复制相似问题