2012-07-01 25 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; 
} 
.... 
相關問題