gpt4 book ai didi

java - GlazedLists AutoCompleteSupport 和搜索自定义对象

转载 作者:行者123 更新时间:2023-11-30 07:07:49 25 4
gpt4 key购买 nike

我试图让 AutoCompleteSupport 搜索对象中的特定字符串,但是当我将 EventList 分配给 JComboBox 时,我不明白如何指示安装程序仅查询 Station 对象的“标题”属性.相反,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,但这根本不起作用。

最佳答案

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

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

关键方面是 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());
}
}
}

关于java - GlazedLists AutoCompleteSupport 和搜索自定义对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24560845/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com