Senin, 08 September 2025

JAVA - Swing Search Sort JList

 





package mainframe;

import javax.swing.*;

 

/**

 * SwingSearchSortJListExample.java

 * Launches the MainFrame in the Swing worker thread

 * @author www.codejava.net

 */

public class SwingSearchSortJListExample {

 

    public static void main(String[] args) {

        SwingUtilities.invokeLater(new Runnable() {

            public void run() {

                new MainFrame().setVisible(true);

            }

        });

    }

}

======================================

package mainframe;

import javax.swing.*;

import java.util.*;

 

/**

 * CustomListModel.java

 * This is an implementation of ListModel that supports using a List

 * collection as the underlying data.

 * @author www.codejava.net

 */

public class CustomListModel<E> extends AbstractListModel<E> {

    protected List<E> list;

 

    public CustomListModel(List<E> list) {

        this.list = list;

    }

 

    public void addElement(E element) {

        list.add(element);

        int index = list.size();

        fireContentsChanged(element, index, index);

    }

 

    public void fireDataChanged() {

        int index = list.size();

        fireContentsChanged(list.get(index - 1), index, index);

    }

 

    public int getSize() {

        return list.size();

    }

 

    public E getElementAt(int index) {

        return list.get(index);

    }

}

================================================

File : Person.java

package mainframe;

public class Person implements Comparable<Person> {

    protected String name;

 

    public Person() {

    }

 

    public Person(String name) {

        setName(name);

    }

 

    public String getName() {

        return this.name;

    }

 

    public void setName(String name) {

        if (name == null) {

            throw new IllegalArgumentException();

        }

 

        this.name = name;

    }

 

    public String toString() {

        return this.name;

    }

 

    public int compareTo(Person another) {

        return this.name.compareTo(another.getName());

    }

 

    public boolean equals(Object obj) {

        if (obj == null) return false;

        if (obj instanceof Person) {

            Person another = (Person) obj;

            if (this.name.equals(another.getName())) {

                return true;

            }

        }

 

        return false;

    }

 

    public int hashCode() {

        return this.name.hashCode();

    }

}

============================================

package mainframe;

import java.awt.*;

import java.awt.event.*;

import javax.swing.*;

import java.util.*;

 

 

/**

 * MainFrame.java

 * This is the main user interface of the application.

 * @author www.codejava.net

 */

public class MainFrame extends JFrame {

    protected JButton buttonAdd = new JButton("Add New Person");

    protected JButton buttonSearch = new JButton("Search Persons");

    protected JButton buttonSort = new JButton("Sort Persons");

 

    protected JList<Person> listPerson = new JList<>();

    protected CustomListModel<Person> listModel;

    protected java.util.List<Person> persons = new ArrayList<>();

 

    public MainFrame() {

        super("Swing Search & Sort Example");

 

        initComponents();

 

        setSize(600, 480);

        setLocationRelativeTo(null);

        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    }

 

    protected void initComponents() {

        setLayout(new FlowLayout(FlowLayout.CENTER, 10, 10));

        JPanel panelButton = new JPanel();

        panelButton.setLayout(new FlowLayout(FlowLayout.CENTER));

 

        buttonAdd.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent evt) {

                addPerson();

            }

        });

 

        buttonSort.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent evt) {

                sortPersons();

            }

        });

 

        buttonSearch.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent evt) {

                searchPersons();

            }

        });

 

        panelButton.add(buttonAdd);

        panelButton.add(buttonSearch);

        panelButton.add(buttonSort);

 

        add(panelButton);

 

        listPerson.setPreferredSize(new Dimension(400, 360));

 

        listModel = new CustomListModel<Person>(persons);

        listPerson.setModel(listModel);

 

        listModel.addElement(new Person("John Doe"));

 

        add(listPerson);

    }

 

    private void addPerson() {

        String personName = JOptionPane.showInputDialog(this, "Enter person name");

        if (personName != null) {

            listModel.addElement(new Person(personName));

        }

    }

 

    private void sortPersons() {

        Collections.sort(persons);

        listModel.fireDataChanged();

    }

 

    private void searchPersons() {

        String personName = JOptionPane.showInputDialog(this, "Enter person name to search for:");

 

        if (personName == null) {

            return;

        }

 

        Collections.sort(persons);

 

        int foundIndex = Collections.binarySearch(persons, new Person(personName));

 

        if (foundIndex >= 0) {

            listPerson.setSelectedIndex(foundIndex);

        } else {

            JOptionPane.showMessageDialog(this, "Could not find the person " + personName);

        }

    }


}

=======================================



Tidak ada komentar: