gpt4 book ai didi

java - Glazedlists:在 FilterList 中执行操作后获取结果

转载 作者:行者123 更新时间:2023-12-01 12:59:55 24 4
gpt4 key购买 nike

我有一个 jtable,可以使用 Glazedlists 的 TextComponentMatcherEditor 进行过滤...并且我有一个文本字段,显示该表的行数...应用过滤器后如何获取行数?在过滤器上插入的每个键,必须更新 textFiled...

我希望我的问题很清楚,谢谢

最佳答案

大概您在某处有一个 FilterList ,其作用是处理源事件列表和匹配器编辑器之间的过滤。在这种情况下,您只需调用 FilterList.size() 即可获取当前项目的数量。

当然,对列表更改使用react是基本步骤。因为 GlazedLists 都是关于列表上的“事件”,所以您不会感到惊讶,因为有一些简单的方法可以监听发生变化的情况,EventList.addListEventListener() 就是一个明显的例子。

我编写了一个 JTable 的小示例程序,它连接 GlazedLists 方式并对文本字段过滤器使用react。 GUI 底部是一个 JLabel,显示当前显示的项目数。

注意,我只再次测试了 GlazedLists v1.8,但我认为回到 v1.5 应该没问题。

import ca.odell.glazedlists.BasicEventList;
import ca.odell.glazedlists.EventList;
import ca.odell.glazedlists.FilterList;
import ca.odell.glazedlists.GlazedLists;
import ca.odell.glazedlists.SortedList;
import ca.odell.glazedlists.TextFilterator;
import ca.odell.glazedlists.event.ListEvent;
import ca.odell.glazedlists.event.ListEventListener;
import ca.odell.glazedlists.gui.TableFormat;
import ca.odell.glazedlists.matchers.ThreadedMatcherEditor;
import ca.odell.glazedlists.swing.EventTableModel;
import ca.odell.glazedlists.swing.TextComponentMatcherEditor;
import java.awt.BorderLayout;
import java.util.List;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;

public class TextFilterCount {


private EventList<Person> eventList = new BasicEventList<Person>();

public TextFilterCount() {
setupGui();
populateTable();
}

private void populateTable() {
eventList.add(new Person("Charles", "Dickens"));
eventList.add(new Person("George", "Orwell"));
eventList.add(new Person("John", "Steinbeck"));
eventList.add(new Person("Jospeh", "Heller"));
}

private void setupGui() {
JFrame frame = new JFrame("GlazedListed TextFilter count example");

final JLabel countLabel = new JLabel();

frame.setSize(600, 400);

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

JTextField textField = new JTextField();

SortedList sortedList = new SortedList(eventList);

TextFilterator<Person> personFilterator = new TextFilterator<Person>() {
@Override
public void getFilterStrings(List baseList, Person person) {
baseList.add(person.getFirstName());
baseList.add(person.getLastName());
}
};

TextComponentMatcherEditor matcherEditor = new TextComponentMatcherEditor(textField, personFilterator);
final FilterList filteredList = new FilterList(sortedList, new ThreadedMatcherEditor(matcherEditor));

filteredList.addListEventListener(new ListEventListener() {

@Override
public void listChanged(ListEvent le) {
System.out.println("List changed.");
countLabel.setText("" + filteredList.size());
}
});

String[] propertyNames = new String[] {"firstName", "lastName"};
String[] columnLabels = new String[] {"First name", "Last name"};

TableFormat tf = GlazedLists.tableFormat(Person.class, propertyNames, columnLabels);
JTable table = new JTable(new EventTableModel(filteredList, tf));

JPanel mainPanel = new JPanel(new BorderLayout());

mainPanel.add(textField, BorderLayout.NORTH);
mainPanel.add(new JScrollPane(table), BorderLayout.CENTER);

mainPanel.add(countLabel, BorderLayout.SOUTH);

frame.getContentPane().add(mainPanel);
frame.setVisible(true);
}


public class Person implements Comparable<Person> {
private String firstName = "";
private String lastName = "";

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

public String getFirstName() {
return firstName;
}

public void setFirstName(String firstName) {
this.firstName = firstName;
}

public String getLastName() {
return lastName;
}

public void setLastName(String lastName) {
this.lastName = lastName;
}

@Override
public int compareTo(Person p) {
if (this.getLastName().equals(p.getLastName())) {
return this.getFirstName().compareTo(p.getFirstName());
}

return this.getLastName().compareTo(p.getLastName());
}
}

public static void main(String[] args) {
new TextFilterCount();
}
}

关于java - Glazedlists:在 FilterList 中执行操作后获取结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23587502/

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