java swing example for searching and sorting a collection of objects using jlist
here's an example of how to use Java Swing and JList to search and sort a collection of objects:
refer:ot theitroad.comimport java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import javax.swing.DefaultListModel;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextField;
public class SearchAndSortExample extends JFrame {
private List<Person> people;
private JList<Person> personList;
private DefaultListModel<Person> listModel;
private JTextField searchField;
public SearchAndSortExample() {
// create some sample data
people = new ArrayList<Person>();
people.add(new Person("Alice", "Smith", 25));
people.add(new Person("Bob", "Jones", 35));
people.add(new Person("Charlie", "Brown", 30));
people.add(new Person("David", "Lee", 40));
// create a list model and populate it with the sample data
listModel = new DefaultListModel<Person>();
for (Person person : people) {
listModel.addElement(person);
}
// create the JList and set its model
personList = new JList<Person>(listModel);
// create a search field and add an action listener to filter the list
searchField = new JTextField(20);
searchField.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
filterList();
}
});
// create a button to sort the list and add an action listener
JButton sortButton = new JButton("Sort by age");
sortButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
sortList();
}
});
// create a panel to hold the search field and sort button
JPanel controlPanel = new JPanel();
controlPanel.add(searchField);
controlPanel.add(sortButton);
// add the JList and control panel to the frame
add(new JScrollPane(personList), BorderLayout.CENTER);
add(controlPanel, BorderLayout.SOUTH);
// set some properties of the frame
setTitle("Search and Sort Example");
setSize(400, 300);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
}
private void filterList() {
String searchText = searchField.getText().toLowerCase();
listModel.clear();
for (Person person : people) {
if (person.getFirstName().toLowerCase().contains(searchText) ||
person.getLastName().toLowerCase().contains(searchText)) {
listModel.addElement(person);
}
}
}
private void sortList() {
Collections.sort(people, new Comparator<Person>() {
public int compare(Person p1, Person p2) {
return p1.getAge() - p2.getAge();
}
});
listModel.clear();
for (Person person : people) {
listModel.addElement(person);
}
}
public static void main(String[] args) {
new SearchAndSortExample();
}
}
class Person {
private String firstName;
private String lastName;
private int age;
public Person(String firstName, String lastName, int age) {
this.firstName = firstName;
this.lastName = lastName;
this.age = age;
}
public String getFirstName() {
return firstName;
}
public String getLastName() {
return lastName;
}
public int getAge() {
return age;
}
public String toString() {
return firstName +
