Ontem eu fiz um, é um pouco grande mas vou postá-lo assim mesmo.
package guj;
import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.util.Random;
import java.util.Set;
import java.util.TreeSet;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.SwingUtilities;
import ca.odell.glazedlists.BasicEventList;
import ca.odell.glazedlists.GlazedLists;
import ca.odell.glazedlists.SortedList;
import ca.odell.glazedlists.swing.AutoCompleteSupport;
import ca.odell.glazedlists.swing.EventComboBoxModel;
import ca.odell.glazedlists.swing.EventTableModel;
import ca.odell.glazedlists.swing.TableComparatorChooser;
import ca.odell.glazedlists.util.concurrent.Lock;
public class ExemploGlazedListsAutocomplete extends JFrame {
public ExemploGlazedListsAutocomplete() {
super();
initialize();
}
private JButton getBtnDelete() {
if (btnDelete == null) {
btnDelete = new JButton();
btnDelete.setText("Delete");
}
return btnDelete;
}
private JButton getBtnNew() {
if (btnNew == null) {
btnNew = new JButton();
btnNew.setText("New");
}
return btnNew;
}
private JButton getBtnRefresh() {
if (btnRefresh == null) {
btnRefresh = new JButton();
btnRefresh.setText("Refresh");
btnRefresh.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent e) {
btnRefresh.setEnabled(false);
refreshCustomers();
btnRefresh.setEnabled(true);
}
});
}
return btnRefresh;
}
private JComboBox getCboSelect() {
if (cboSelect == null) {
cboSelect = new JComboBox();
cboSelect.setModel(customerComboBoxModel);
AutoCompleteSupport.install(cboSelect, customers);
}
return cboSelect;
}
private JPanel getJContentPane() {
if (jContentPane == null) {
jContentPane = new JPanel();
jContentPane.setLayout(new BorderLayout());
jContentPane.add(getPnlTop(), BorderLayout.NORTH);
jContentPane.add(getScpList(), BorderLayout.CENTER);
}
return jContentPane;
}
private JPanel getPnlButtons() {
if (pnlButtons == null) {
pnlButtons = new JPanel();
pnlButtons.setLayout(new FlowLayout());
pnlButtons.add(getBtnNew());
pnlButtons.add(getBtnDelete());
pnlButtons.add(getBtnRefresh());
}
return pnlButtons;
}
private JPanel getPnlTop() {
if (pnlTop == null) {
pnlTop = new JPanel();
pnlTop.setLayout(new BorderLayout());
pnlTop.add(getCboSelect(), BorderLayout.CENTER);
pnlTop.add(getPnlButtons(), BorderLayout.EAST);
}
return pnlTop;
}
private JScrollPane getScpList() {
if (scpList == null) {
scpList = new JScrollPane();
scpList.setViewportView(getTblList());
}
return scpList;
}
private JTable getTblList() {
if (tblList == null) {
tblList = new JTable();
tblList.setModel(customerTableModel);
TableComparatorChooser tableSorter = TableComparatorChooser.install(tblList, customers,
TableComparatorChooser.SINGLE_COLUMN);
}
return tblList;
}
private void initialize() {
this.setSize(605, 259);
this.setContentPane(getJContentPane());
this.setTitle("Exemplo GlazedLists Autocomplete");
refreshCustomers();
}
private void addCustomer() {
String[] firstNames = {
"Joao", "Maria", "Jose", "Pedro", "Paulo", "Severino", "Catarina", "Luisa", "Diana", "Antonio", "Patricia", "Ronaldo"
};
String[] lastNames = {
"da Silva", "de Souza", "Nascimento", "Conceicao", "de Jesus", "dos Santos", "Assuncao", "Riquelme", "Messi", "Nazario"
};
int customerID = r.nextInt(10000);
String customerName = firstNames[r.nextInt(firstNames.length)] + " " + lastNames[r.nextInt(lastNames.length)];
int id = customerID;
String address = String.format("Bingo Street %d", r.nextInt(2000) + 1);
String phone = String.format("(%03d)%04d-%04d", r.nextInt(900) + 100, r.nextInt(10000), r.nextInt(10000));
String ssn = String.format("%03d-%02d-%04d", r.nextInt(1000), r.nextInt(100), r.nextInt(10000));
Lock writeLock = customers.getReadWriteLock().writeLock();
try {
writeLock.lock();
customers.add(new Customer(id, customerName, address, phone, ssn));
} finally {
writeLock.unlock();
}
}
private void refreshCustomers() {
Lock writeLock = customers.getReadWriteLock().writeLock();
try {
writeLock.lock();
customers.clear();
} finally {
writeLock.unlock();
}
int nCustomers = r.nextInt(20) + 5; // entre 5 e 24 clientes
for (int i = 0; i < nCustomers; ++i) {
addCustomer();
}
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
ExemploGlazedListsAutocomplete thisClass = new ExemploGlazedListsAutocomplete();
thisClass.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
thisClass.setVisible(true);
}
});
}
private JButton btnDelete = null;
private JButton btnNew = null;
private JButton btnRefresh = null;
private JComboBox cboSelect = null;
private SortedList<Customer> customers = new SortedList<Customer>(new BasicEventList<Customer>());
private EventComboBoxModel<Customer> customerComboBoxModel = new EventComboBoxModel<Customer>(customers);
private EventTableModel<Customer> customerTableModel = new EventTableModel<Customer>(customers, GlazedLists.tableFormat(
new String[] {
"id", "name", "address", "phone", "ssn"
}, new String[] {
"ID", "Nome", "Endereço", "Telefone", "SSN"
}));
private JPanel jContentPane = null;
private JPanel pnlButtons = null;
private JPanel pnlTop = null;
private Random r = new Random();
private JScrollPane scpList = null;
private JTable tblList = null;
private static final long serialVersionUID = 1L;
}
Usa a seguinte classe (Customer):
package guj;
public class Customer implements Comparable<Customer> {
public Customer() {
}
public Customer(int id, String name, String address, String phone,
String ssn) {
this.id = id;
this.name = name;
this.address = address;
this.phone = phone;
this.ssn = ssn;
}
@Override
public int compareTo(Customer that) {
return Integer.valueOf(id).compareTo(Integer.valueOf(that.id));
}
public String getAddress() {
return address;
}
public int getId() {
return id;
}
public String getName() {
return name;
}
public String getPhone() {
return phone;
}
public String getSsn() {
return ssn;
}
public void setAddress(String address) {
this.address = address;
}
public void setId(int id) {
this.id = id;
}
public void setName(String name) {
this.name = name;
}
public void setPhone(String phone) {
this.phone = phone;
}
public void setSsn(String ssn) {
this.ssn = ssn;
}
@Override public String toString() {
return name;
}
private String address;
private int id;
private String name;
private String phone;
private String ssn;
}
Note que alguns botões estão lá mas não estão implementados (como os botões New e Delete). Entretanto, o botão Refresh cria uma lista nova…
Um exemplo de execução:
