2012-07-01 22 views
2

我有一套单选按钮,我想用作我的桌子的过滤器。这个单选按钮在我的模型类中设置了一个变量。在我的模型中有一个getter,我检索这个值,我想在我的GlazedList表中使用这个值作为过滤器。如何使用GlazedList中的字符串替换JTextField作为过滤器?

有没有人知道该怎么做?

下面是我的表的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); 

回答

2

我想指出你的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 S:

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; 
} 
.... 
相关问题