gpt4 book ai didi

java - 如何将 JList 绑定(bind)到 Bean 类属性

转载 作者:塔克拉玛干 更新时间:2023-11-03 05:26:56 25 4
gpt4 key购买 nike

我尝试将 JList 绑定(bind)到绑定(bind)类属性 Vector。在绑定(bind)类中,当 UI 中的 JTextField 更新时,Vector 也会更新。

public void setName(String name) {
String oldName = this.name;
this.name = name;
Vector oldList=this.list;
list.add(name);
propertySupport.firePropertyChange("name", oldName, this.name); //textField updates
propertySupport.firePropertyChange(PROP_LIST, oldList, list); // JList
}

还有另一个单独的 setter 可用于更新 Vector。我也设置了添加/删除 PropertyChangeListeners。

我真正的需求是根据BeanBinding 类中的数据更新JList。例如,当用户输入JTextField时,数据库中的相关数据和类中已有的变量应该加载到JList中。

请告诉我如何在 NetBeans 中为 JList 绑定(bind)数据 BeanBinding 类(源)属性。至少任何教程链接。 NetBeans 站点中有一个示例,但它用于直接从数据库中获取数据。

最佳答案

不了解 Netbeans,只是简单的编码 :-) 在那里,将您的列表包装到一个 ObservableList 中,并对 observable 进行所有更改应该有效。这是一个可运行的代码片段(抱歉太长了,现在没有精力将其剥离)

public class SimpleListBinding {
@SuppressWarnings("unused")
private static final Logger LOG = Logger
.getLogger(SimpleListBinding.class.getName());
private JComponent content;
private JList list;
private JTextField textField;

private List<MyBean> beanList;
private JButton modifyButton;
private JButton addButton;


/**
* Binds list and simple properties.
*/
private void bindBasics() {
BindingGroupBean context = new BindingGroupBean();
JListBinding listBinding = SwingBindings.createJListBinding(UpdateStrategy.READ_WRITE,
beanList, list);
listBinding.setDetailBinding(BeanProperty.create("value"));
context.addBinding(listBinding);
context.addBinding(Bindings.createAutoBinding(UpdateStrategy.READ_WRITE,
list, BeanProperty.create("selectedElement.value"),
textField, BeanProperty.create("text")));
context.bind();
Action action = new AbstractAction("programatically change") {
public void actionPerformed(ActionEvent e) {
int selectedBean = list.getSelectedIndex();
if (selectedBean < 0) return;
MyBean bean = beanList.get(selectedBean);
bean.setValue(bean.getValue() + "*");
}

};
modifyButton.setAction(action);

Action add = new AbstractAction("add bean") {
int count = 0;
@Override
public void actionPerformed(ActionEvent e) {
beanList.add(new MyBean("added: " + count++));
}

};
addButton.setAction(add);
}

private void initData() {
MyBean[] beans = new MyBean[] {
new MyBean("first"), new MyBean("second"), new MyBean("third")
};
beanList = ObservableCollections.observableList(new ArrayList(Arrays.asList(beans)));
}

public static class MyBean extends AbstractBean {
private String value;
private boolean active;
public MyBean(String value) {
this.value = value;
}
public String getValue() {
return value;
}

public void setValue(String value) {
Object old = getValue();
this.value = value;
firePropertyChange("value", old, getValue());
}

public void setActive(boolean active) {
boolean old = getActive();
this.active = active;
firePropertyChange("active", old, getActive());
}

public boolean getActive() {
return active;
}
}

private JComponent getContent() {
if (content == null) {
initComponents();
content = build();
initData();
bindBasics();
}
return content;
}

private JComponent build() {
JComponent comp = Box.createVerticalBox();
comp.add(new JScrollPane(list));
comp.add(textField, BorderLayout.SOUTH);
comp.add(modifyButton);
comp.add(addButton);
return comp;
}

private void initComponents() {
list = new JList();
textField = new JTextField();
modifyButton = new JButton("modify programmtically");
addButton = new JButton();
}

public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
final JXFrame frame = new JXFrame("List binding", true);
frame.add(new SimpleListBinding().getContent());
frame.pack();
frame.setSize(400, 300);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}

}

编辑:用 JList 替换 JXList(行为上没有区别,只是为了使其可编译:-)

关于java - 如何将 JList 绑定(bind)到 Bean 类属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7411497/

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