gpt4 book ai didi

java - 如何用 GlazedList 中的字符串替换 JTextField 作为过滤器?

转载 作者:行者123 更新时间:2023-11-30 09:35:37 26 4
gpt4 key购买 nike

我有一组单选按钮,我想将其用作我的表格的过滤器。这个单选按钮在我的模型类中设置了一个变量。在我的模型中使用 setter/getter ,我检索了这个值,我想在我的 GlazedList 表中使用这个值作为过滤器。

有人知道怎么做吗?

下面是我用 JTextField 作为过滤器的表格:

TextFilterator<Barcode> barcodeFilterator = new TextFilterator<Barcode>() { ... };
WebTextField searchField = new WebTextField(barcodeModel.getSelectedFilter());
MatcherEditor<Barcode> textMatcherEditor = new TextComponentMatcherEditor<Barcode>(searchField, barcodeFilterator);
FilterList<Barcode> filterList = new FilterList<Barcode>(BarcodeUtil.retrieveBarcodeEventList(files), textMatcherEditor);
TableFormat<Barcode> tableFormat = new TableFormat<Barcode>() { .... };
EventTableModel<Barcode> tableModel = new EventTableModel<Barcode>(filterList, tableFormat);
barcodeTable.setModel(tableModel);

最佳答案

我会向您指出 Custom MatcherEditor screencast作为实现您自己的 Matcher 以应对从一组选项中进行过滤的良好引用。

关键部分是创建 MatcherEditor,在本例中,它按国籍过滤人员表。

private static class NationalityMatcherEditor extends AbstractMatcherEditor implements ActionListener {
private JComboBox nationalityChooser;

public NationalityMatcherEditor() {
this.nationalityChooser = new JComboBox(new Object[] {"British", "American"});
this.nationalityChooser.getModel().setSelectedItem("Filter by Nationality...");
this.nationalityChooser.addActionListener(this);
}

public Component getComponent() {
return this.nationalityChooser;
}

public void actionPerformed(ActionEvent e) {
final String nationality = (String) this.nationalityChooser.getSelectedItem();
if (nationality == null)
this.fireMatchAll();
else
this.fireChanged(new NationalityMatcher(nationality));
}

private static class NationalityMatcher implements Matcher {
private final String nationality;

public NationalityMatcher(String nationality) {
this.nationality = nationality;
}

public boolean matches(Object item) {
final AmericanIdol idol = (AmericanIdol) item;
return this.nationality.equals(idol.getNationality());
}
}
}

这个MatcherEditor的使用方式应该不会太陌生,因为它类似于TextMatcherEditor:

EventList idols = new BasicEventList();
NationalityMatcherEditor nationalityMatcherEditor = new NationalityMatcherEditor();
FilterList filteredIdols = new FilterList(idols, nationalityMatcherEditor);

在上面的示例中,JComboBox 是在 MatcherEditor 本身中声明和启动的。您无需完全遵循该样式,尽管您需要对所跟踪对象的引用。对我来说,如果我正在观看 Swing 控件,我倾向于声明并启动表单的其余部分,然后传递一个引用,例如

....
private JComboBox nationalityChooser;
public NationalityMatcherEditor(JComboBox alreadyConfiguredComboBox) {
this.nationalityChooser = alreadyConfiguredComboBox;
}
....

关于java - 如何用 GlazedList 中的字符串替换 JTextField 作为过滤器?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11285090/

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