我试图让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,但这根本不起作用。
答案 0 :(得分: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());
}
}
}