[英]Java GlazedList individual column filtering
我目前在單獨的列標題中有一個帶有文本字段的表格,該表格會根據它們的每個單獨的列進行過濾。.我正嘗試使用釉面列表,但是我該怎么做呢? 每列都應有一個單獨的文本字段,並且過濾是基於所有基於單獨列進行過濾的文本字段進行的。 EG:帶有“名字”和“姓氏”列的表。 該表將根據姓氏過濾器基於名字過濾人員的結果,並根據姓氏過濾器過濾姓氏
首先需要了解的是GlazedLists主要是關於處理對象列表的-線索就是名字。 許多人將其視為一種向表和列表添加排序和篩選功能的庫,這肯定會引起麻煩。
因此,首先關注GlazedLists將為您提供一種稱為EventList
的特殊類型的列表結構這一事實,它是您的核心數據結構; 從那里,您將獲得有用的方法來進行轉換,排序,過濾等。然后,由於它非常龐大,因此可以使用易於使用的類將EventList鏈接到JList或JTable。
一旦將所有這些連接起來,對列表的更改和轉換將自動傳播到與其鏈接的任何Swing組件。
無論如何,這是一個示例類,它將向您展示如何創建EventList,然后通過Swing組件應用文本過濾器,然后將其全部鏈接到Swing表。 (這不涉及排序或如何獲取所選項目,但是文檔非常出色。)
使用Java 9和GlazedLists 1.11進行了測試。
import ca.odell.glazedlists.*; import ca.odell.glazedlists.gui.TableFormat; import ca.odell.glazedlists.matchers.MatcherEditor; import ca.odell.glazedlists.swing.DefaultEventSelectionModel; import ca.odell.glazedlists.swing.DefaultEventTableModel; import ca.odell.glazedlists.swing.TextComponentMatcherEditor; import javax.swing.*; import java.awt.*; import java.util.List; public class GlazedListsMultipleTextfields { private JFrame frame; private JTable table; private JTextField txtFirstName; private JTextField txtLastName; private EventList people; private DefaultEventSelectionModel selectionModel; public GlazedListsMultipleTextfields() { setupGui(); setupGlazedLists(); populatedList(); frame.setVisible(true); } private void setupGui() { frame = new JFrame("GlazedLists Multiple Filter Examples"); frame.setSize(600, 600); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // Create the panel to hold the input textfields txtFirstName = new JTextField(); JPanel pnlFirstName = new JPanel(new BorderLayout()); pnlFirstName.add(new JLabel("First name"), BorderLayout.WEST); pnlFirstName.add(txtFirstName, BorderLayout.CENTER); txtLastName = new JTextField(); JPanel pnlLastName = new JPanel(new BorderLayout()); pnlLastName.add(new JLabel("Last name"), BorderLayout.WEST); pnlLastName.add(txtLastName, BorderLayout.CENTER); JPanel textInputs = new JPanel(); textInputs.setLayout(new BoxLayout(textInputs, BoxLayout.Y_AXIS)); textInputs.add(pnlFirstName); textInputs.add(pnlLastName); table = new JTable(); frame.getContentPane().add(new JScrollPane(table), BorderLayout.CENTER); frame.getContentPane().add(textInputs, BorderLayout.NORTH); } 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")); people.add(new Person("John", "Green")); } private void setupGlazedLists() { people = new BasicEventList(); MatcherEditor firstNameMatcherEditor = new TextComponentMatcherEditor(txtFirstName, new FirstNameTextFilterator()); MatcherEditor lastNameMatcherEditor = new TextComponentMatcherEditor(txtLastName, new LastNameTextFilterator()); FilterList filteredFirstNames = new FilterList(people, firstNameMatcherEditor); FilterList filteredLastNames = new FilterList(filteredFirstNames, lastNameMatcherEditor); TableFormat tableFormat = GlazedLists.tableFormat(new String[]{"firstName", "lastName"}, new String[]{"First Name", "Last Name"}); DefaultEventTableModel model = new DefaultEventTableModel(filteredLastNames, tableFormat); selectionModel = new DefaultEventSelectionModel(filteredLastNames); table.setModel(model); table.setSelectionModel(selectionModel); } class FirstNameTextFilterator implements TextFilterator { @Override public void getFilterStrings(List list, Person person) { list.add(person.getFirstName()); } } class LastNameTextFilterator implements TextFilterator { @Override public void getFilterStrings(List list, Person person) { list.add(person.getLastName()); } } public class 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 String getLastName() { return lastName; } } public static void main(String[] args) { javax.swing.SwingUtilities.invokeLater(new Runnable() { @Override public void run() { new GlazedListsMultipleTextfields(); } }); } }
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.