gpt4 book ai didi

java Swing——JPanel 和 PropertyChangeListener

转载 作者:行者123 更新时间:2023-11-30 04:51:27 25 4
gpt4 key购买 nike

我的用例是 List<String>被传递到 Jpanel对于每个 StringListJPanel呈现 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();
}

}

该代码适用于所有 Strings在过去List 。我希望我的小组能够了解 List 可能发生的任何更改如添加或删除,并相应地添加/删除相关的相应 UI 组件。我认为这可以通过使用 PropertyChangeListener 来完成,但无法在我的代码中解释这一点。
关于如何在 List 发生更改后立即使我的面板渲染/重新渲染内容的任何想法或建议会有帮助。

最佳答案

这里你需要的是一个可观察的集合。这应该可以做到: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
}
}
}

顺便说一句:另一个有助于将业务对象绑定(bind)到 GUI 并对修改使用react(双向)的概念是数据绑定(bind)。看看this ,一个常与 Swing 一起使用的数据绑定(bind)库。

关于java Swing——JPanel 和 PropertyChangeListener,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9753605/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com