I am trying to get AutoCompleteSupport to search for a specific string in an object, but when I assign the EventList to the JComboBox, I don't understand how to instruct the installer to query only the Station object's "title" property only. Instead the AutoCompleteSupport searches the entire object name string. Is there another thing that I need to implement, to tell the AutoCompleteSupport to search only across this one set of properties in a particular object?
Code thus far:
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());
}
});
}
}
This is the Station object:
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;
}
}
I've tried implementing FilterableText within the Station object, but that didn't work at all.
I don't quite understand why you're trying to extend the combobox in order to install the auto-complete support.
Here's a small, yet complete sample program. I didn't know what type of station you were referring to so in my example I'm thinking about radio stations in the UK. I'm listing their name and their location.
The key aspect is the TextFilterator. If you tried installing without the filterator then you'd find that the auto-complete would also match the location too (because that's output in the toString() and by default that's where the filtering is taking place). However, once it is included I can specify precisely the fields of interest - ie title - and only the station title is matched in the filtering.
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());
}
}
}