我不太明白您为什么要尝试扩展组合框以安装自动完成支持。
这是一个小而完整的示例程序。我不知道您指的是哪种类型的电台,所以在我的示例中,我正在考虑英国的广播电台。我列出了他们的名字和他们的位置。
关键方面是 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());
}
}
}