【问题标题】:GlazedLists AutoCompleteSupport and searching custom objectsGlazedLists AutoCompleteSupport 和搜索自定义对象
【发布时间】:2014-08-24 23:58:37
【问题描述】:

我试图让 AutoCompleteSupport 在对象中搜索特定字符串,但是当我将 EventList 分配给 JComboBox 时,我不明白如何指示安装程序仅查询 Station 对象的“title”属性.相反,AutoCompleteSupport 会搜索整个对象名称字符串。我还需要实现另一件事,告诉 AutoCompleteSupport 只搜索特定对象中的这一组属性吗?

到目前为止的代码:

public class StationFinder extends JComboBox {
    private EventList<Station> stations = new BasicEventList<Station>();

    public StationFinder() {
        setStations(); // this sets up the 'stations' property

        SwingUtilities.invokeLater(new Runnable() {

            public void run() {
                 AutoCompleteSupport.install(StationFinder.this, getStations());
            }

        });
    }
}

这是 Station 对象:

public class Station {
private int id;
private String metaName;
private String title;

...

public int getId() {
    return id;
}

public void setId(int id) {
    this.id = id;
}

public String getMetaName() {
    return metaName;
}

public void setMetaName(String metaName) {
    this.metaName = metaName;
}

public String getTitle() {
    return title;
}

public void setTitle(String title) {
    this.title = title;
}

}

我尝试在 Station 对象中实现 FilterableText,但这根本不起作用。

【问题讨论】:

    标签: java autocomplete jcombobox glazedlists


    【解决方案1】:

    我不太明白您为什么要尝试扩展组合框以安装自动完成支持。

    这是一个小而完整的示例程序。我不知道您指的是哪种类型的电台,所以在我的示例中,我正在考虑英国的广播电台。我正在列出他们的姓名和位置。

    关键方面是 TextFilterator。如果您尝试在不使用过滤器的情况下进行安装,那么您会发现自动完成功能也会匹配该位置(因为它是 toString() 中的输出,默认情况下这是进行过滤的地方)。但是,一旦包含它,我就可以精确地指定感兴趣的字段 - 即标题 - 并且只有电台标题在过滤中匹配。

    import ca.odell.glazedlists.BasicEventList;
    import ca.odell.glazedlists.EventList;
    import ca.odell.glazedlists.TextFilterator;
    import ca.odell.glazedlists.matchers.TextMatcherEditor;
    import ca.odell.glazedlists.swing.AutoCompleteSupport;
    import ca.odell.glazedlists.swing.EventComboBoxModel;
    import java.awt.BorderLayout;
    import java.util.List;
    import javax.swing.JComboBox;
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    
    public class StationFinderAutoComplete {
    
        private JFrame mainFrame;
        private JComboBox stationsComboBox;
        private EventList<Station> stations = new BasicEventList<Station>();
    
        public StationFinderAutoComplete() {
            populateStations();
            createGui();
            mainFrame.setVisible(true);
        }
    
        private void populateStations() {
            stations.add(new Station("Key 103", "Manchester"));
            stations.add(new Station("Capital FM", "London"));
            stations.add(new Station("BBC Radio Leeds", "Leeds"));
            stations.add(new Station("BBC Radio 4", "London"));
        }
    
        private void createGui() {
            mainFrame = new JFrame("GlazedLists Autocomplete Example");
            mainFrame.setSize(600, 400);
            mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    
            // Use a GlazedLists EventComboBoxModel to connect the JComboBox with an EventList.
            EventComboBoxModel<Station> model = new EventComboBoxModel<Station>(stations);
            stationsComboBox = new JComboBox(model);
    
            AutoCompleteSupport autocomplete = AutoCompleteSupport.install(stationsComboBox, stations, new StationTextFilterator());
            // Try without the filterator to see the difference.
            //AutoCompleteSupport autocomplete = AutoCompleteSupport.install(stationsComboBox, stations);
            autocomplete.setFilterMode(TextMatcherEditor.CONTAINS);
    
            JPanel panel = new JPanel(new BorderLayout());
            panel.add(stationsComboBox, BorderLayout.NORTH);
            mainFrame.setLayout(new BorderLayout());
            mainFrame.getContentPane().add(panel, BorderLayout.CENTER);
    
        }
    
        public static void main(String[] args) {
            javax.swing.SwingUtilities.invokeLater(new Runnable() {
                @Override
                public void run() {
                    new StationFinderAutoComplete();
                }
            });
        }
    
        class Station {
    
            private String title;
            private String location;
    
            public Station(String title, String location) {
                this.title = title;
                this.location = location;
            }
    
            public String getTitle() {
                return title;
            }
    
            public void setTitle(String title) {
                this.title = title;
            }
    
            public String getLocation() {
                return location;
            }
    
            public void setLocation(String location) {
                this.location = location;
            }
    
            @Override
            public String toString() {
                return title + " (" + location + ")";
            }
        }
    
        class StationTextFilterator implements TextFilterator<Station> {
            @Override
            public void getFilterStrings(List<String> baseList, Station station) {
                baseList.add(station.getTitle());
            }
        }
    }
    

    【讨论】:

    • 我的意思是AutoCompleteSupport.install的代码行。是的,所有这些包都可以在标准的 GlazedLists 包中使用。我的观点是你是否将 TextFilterator 传递给 AutoCompleteSupport.install() 方法,与你在系统上安装包无关。
    • 抱歉找到了。正在寻找扩展,而不是实现。 =)
    • @ChrisWalsh 我的示例或解释中是否缺少某些内容,导致您无法将其标记为已回答?如有必要,我很乐意扩展我的答案。
    • 好的,我认为您的代码有效。 =)我删除了你提到的TextFilterator,没有有效的区别。我不太明白那部分,但不管有没有,它的工作原理都是一样的。谢谢!现在开始监听选择事件 =D
    • 这不应该发生。在我的示例代码中,如果您使用注释掉的 AutoCompleteSupport.install() 方法(无过滤器)并尝试输入“伦敦”,您将匹配两个以伦敦为位置的车站。如果您恢复使用带有过滤器的原始安装方法并使用“London”重试,您将找不到匹配项。这就是区别——当你自己跑步时,你看到了吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-11-26
    • 2011-01-11
    • 2011-03-18
    相关资源
    最近更新 更多