gpt4 book ai didi

java - GlazedList 更新 JTable 中的 EventList

转载 作者:行者123 更新时间:2023-12-02 04:13:57 25 4
gpt4 key购买 nike

如何更新 EventList 列表来更新 JTable?这就是我所做的:

        String[] headers = new String[]{"MNO", "NAME", "ID/REG No", "PHONE"};
String[] properties = new String[]{"milkNo", "fullName", "nationalId", "phone1"};


TextFilterator<Member> personTextFilterator = new TextFilterator<Member>() {

@Override
public void getFilterStrings(List list, Member m) {
list.add(m.getFullName());
list.add(m.getMilkNo());
list.add(m.getNationalId());
list.add(m.getPhone1());
}
};

MatcherEditor<Member> textMatcherEditor = new TextComponentMatcherEditor<Member>(txtFilter, personTextFilterator);

FilterList<Member> filterList = new FilterList<Member>(eventList, textMatcherEditor);

TableFormat tf = GlazedLists.tableFormat(properties, headers);
model = new EventTableModel<Member>(filterList, tf);

selectionModel = new EventSelectionModel<Member>(filterList);
tblMembers.setSelectionModel(selectionModel);

tblMembers.setModel(model);

问题是当我过滤表中的记录并选择一条记录并尝试更新它时,它会在表中创建一条新记录

int updatedRow = tblMembers.convertRowIndexToModel(tblMembers.getSelectedRow());
eventList.set(updatedRow, updatedMember);

最佳答案

一旦您使用 GlazedLists 为您的 JTable 提供支持,那么您应该只使用 EventTableModelEventSelectionModel 与您的 JTable 交互。您所犯的错误是直接在 JTable 中查询所选行并获取其索引 - 但 JTable 不明白它是由可能对其中的项目进行排序/过滤的 EventList 支持的。因此,JTable 中的行 i 不一定对应于 EventList 中的元素 i

在您的代码中,您实际上已经设置了一个 EventSelectionModel,现在只需使用它即可。

if (!selectionModel.isSelectionEmpty()) {
EventList<Member> selectedMembers = selectionModel.getSelected();
for (int i = 0; i<selectedMembers.size(); i++) {
Member member = selectedMembers.get(i);
//update accordingly...
//member.setXXX(...);
selectedMembers.set(i, member);
}
}

您必须记住,JTable 支持多行选择(尽管可以配置为仅允许单行选择),因此 EventSelectionModel 会明智地返回所有选定行的 EventList,即使只选择了一行。因此,您需要迭代返回的列表。还有一个额外的便利,即包含所选项目的 EventList 由源 EventList 支持,因此您可以直接对该子列表进行更改。

高级

当然,事件列表非常适合观察列表的基本变化:删除、插入、更新(即用另一个对象替换索引i处的现有对象)。但是,如果我们可以直接更新 EventList 中的对象,然后让 GlazedLists 检测该属性更改,而不是调用 selectedMembers.set(),不是很好吗?

嗯,这也是可能的。有一个名为 ObservableElementList 的有用列表类型,它将监听其包含的每个对象的属性更改并相应地刷新。这比必须在列表中找到对象并调用 eventList.set() 方便得多。

您首先需要使您的类支持属性监听器,即是一个正确的 Java bean,然后使用 ObservableElementList.Connector

我已经创建了一个小而完整的示例。在此示例中,顶部有一个用于过滤的文本输入,一个包含简短作者列表的表格,以及底部的一个按钮,用于更新任何选定行的名称。

import ca.odell.glazedlists.BasicEventList;
import ca.odell.glazedlists.EventList;
import ca.odell.glazedlists.FilterList;
import ca.odell.glazedlists.GlazedLists;
import ca.odell.glazedlists.ObservableElementList;
import ca.odell.glazedlists.TextFilterator;
import ca.odell.glazedlists.matchers.MatcherEditor;
import ca.odell.glazedlists.swing.EventSelectionModel;
import ca.odell.glazedlists.swing.EventTableModel;
import ca.odell.glazedlists.swing.TextComponentMatcherEditor;
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.beans.PropertyChangeListener;
import java.beans.PropertyChangeSupport;
import java.util.List;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;

public class GlazedListSelectionObservable {

private JFrame frame;
private JTable table;
private JTextField txtInput;

private EventList<Person> people;
private EventSelectionModel<Person> selectionModel;

public GlazedListSelectionObservable() {

setupGui();
setupGlazedLists();
populatedList();
frame.setVisible(true);
}

private void setupGui() {

frame = new JFrame("GlazedLists Selection Example");
frame.setSize(600, 600);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

txtInput = new JTextField();
table = new JTable();
frame.getContentPane().add(new JScrollPane(table), BorderLayout.CENTER);

JButton updateTableButton = new JButton("Update selected row");

updateTableButton.addActionListener(new ActionListener() {

@Override
public void actionPerformed(ActionEvent e) {
if (!selectionModel.isSelectionEmpty()) {
EventList<Person> selectedPeople = selectionModel.getSelected();
for (Person person: selectedPeople) {
person.setFirstName("David");
person.setLastName("Baldacci");
}
}
}
});

frame.getContentPane().add(txtInput, BorderLayout.NORTH);
frame.getContentPane().add(updateTableButton, BorderLayout.SOUTH);

}

private void populatedList() {
people.add(new Person("John", "Grisham"));
people.add(new Person("Patricia", "Cornwell"));
people.add(new Person("Nicholas", "Sparks"));
people.add(new Person("Andy", "Weir"));
people.add(new Person("Elizabeth", "George"));
}

private void setupGlazedLists() {
people = new BasicEventList<Person>();
MatcherEditor<Person> textMatcherEditor = new TextComponentMatcherEditor<Person>(txtInput, new PersonTextFilterator());

ObservableElementList.Connector<Person> personConnector = GlazedLists.beanConnector(Person.class);
EventList<Person> observedPeople = new ObservableElementList<Person>(people, personConnector);

FilterList<Person> filteredPeople = new FilterList<Person>(observedPeople, textMatcherEditor);

EventTableModel model = new EventTableModel(filteredPeople, GlazedLists.tableFormat(new String[]{"firstName", "lastName"} , new String[]{"First Name", "Last Name"}));

selectionModel = new EventSelectionModel<Person>(filteredPeople);

table.setModel(model);
table.setSelectionModel(selectionModel);
}

class PersonTextFilterator implements TextFilterator<Person> {

@Override
public void getFilterStrings(List<String> list, Person person) {
list.add(person.getFirstName());
list.add(person.getLastName());
}

}

public class Person {

private String firstName;
private String lastName;

private final PropertyChangeSupport support = new PropertyChangeSupport(this);

public Person() {
}

public Person(String firstName, String lastName) {
this.firstName = firstName;
this.lastName = lastName;
}

public String getFirstName() {
return firstName;
}

public String getLastName() {
return lastName;
}

public void setFirstName(String firstName) {
final String oldFirstName = this.firstName;
this.firstName = firstName;
support.firePropertyChange("firstName", oldFirstName, firstName);
}

public void setLastName(String lastName) {
final String oldLastName = this.lastName;
this.lastName = lastName;
support.firePropertyChange("lastName", oldLastName, lastName);
}

public void addPropertyChangeListener(PropertyChangeListener l) {
support.addPropertyChangeListener(l);
}

public void removePropertyChangeListener(PropertyChangeListener l) {
support.removePropertyChangeListener(l);
}

}

public static void main(String[] args) {
javax.swing.SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
new GlazedListSelectionObservable();
}
});
}

}

关于java - GlazedList 更新 JTable 中的 EventList,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33515611/

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