2

我试图让 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,但这根本不起作用。

4

1 回答 1

4

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

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

关键方面是 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());
        }
    }
}
于 2014-07-04T13:01:37.333 回答