C
C
CheshKin2015-06-21 13:53:55
Java
CheshKin, 2015-06-21 13:53:55

JTable. How to implement a filter?

There is a table that translates a .txt file into a JTable. How to implement search (filter) on JTable?
There is a code that works, but I (due to my illiteracy) cannot add it to the program

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.RowFilter;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableModel;
import javax.swing.table.TableRowSorter;

public class RegexTable {
  public static void main(String args[]) {
    JFrame frame = new JFrame("Regexing JTable");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    Object rows[][] = { { "A", "About", 44.36 }, { "B", "Boy", 44.84 }, { "C", "Cat", 463.63 },
        { "D", "Day", 27.14 }, { "E", "Eat", 44.57 }, { "F", "Fail", 23.15 },
        { "G", "Good", 4.40 }, { "H", "Hot", 24.96 }, { "I", "Ivey", 5.45 },
        { "J", "Jack", 49.54 }, { "K", "Kids", 280.00 } };
    String columns[] = { "Symbol", "Name", "Price" };
    TableModel model = new DefaultTableModel(rows, columns) {
      public Class getColumnClass(int column) {
        Class returnValue;
        if ((column >= 0) && (column < getColumnCount())) {
          returnValue = getValueAt(0, column).getClass();
        } else {
          returnValue = Object.class;
        }
        return returnValue;
      }
    };

    final JTable table = new JTable(model);
    final TableRowSorter<TableModel> sorter = new TableRowSorter<TableModel>(model);
    table.setRowSorter(sorter);
    JScrollPane pane = new JScrollPane(table);
    frame.add(pane, BorderLayout.CENTER);

    JPanel panel = new JPanel(new BorderLayout());
    JLabel label = new JLabel("Filter");
    panel.add(label, BorderLayout.WEST);
    final JTextField filterText = new JTextField("A");
    panel.add(filterText, BorderLayout.CENTER);
    frame.add(panel, BorderLayout.NORTH);
    JButton button = new JButton("Filter");
    button.addActionListener(new ActionListener() {
      public void actionPerformed(ActionEvent e) {
        String text = filterText.getText();
        if (text.length() == 0) {
          sorter.setRowFilter(null);
        } else {
          sorter.setRowFilter(RowFilter.regexFilter(text));
        }
      }
    });
    frame.add(button, BorderLayout.SOUTH);
    frame.setSize(300, 250);
    frame.setVisible(true);
  }

}

And here is the program in
which you need to add a search
package sample;
import java.io.FileNotFoundException;
import java.io.IOException;

public class Main {

    public static void main(String[] arg) throws FileNotFoundException, IOException
    {
        PersonJFrame frame = new PersonJFrame("People");
    }
}

package sample;

public class Person {

    private String name;
    private String surname;
    private int age;
    private String gender;

    public Person(String name, String surname, int age, String gender) //Параметры: имя, фамилия, возраст, пол
    {
        this.name = name;
        this.surname = surname;
        this.age = age;
        this.gender = gender;
    }

    public String getName() { return name; }
    public void setName(String name) { this.name = name; }

    public String getSurname() { return surname; }
    public void setSurname(String surname) { this.surname = surname; }

    public int getAge() { return age; }
    public void setAge(int age) { this.age = age; }

    public String getGender() { return gender; }
    public void setGender(String gender) { this.gender = gender; }
}

package sample;
        import java.awt.BorderLayout;
        import java.awt.Dimension;
        import java.awt.Toolkit;
        import java.io.BufferedReader;
        import java.io.FileNotFoundException;
        import java.io.FileReader;
        import java.io.IOException;
        import java.util.ArrayList;
        import javax.swing.JFrame;
        import javax.swing.JScrollPane;
        import javax.swing.JTable;

public class PersonJFrame extends JFrame
{
    private JScrollPane scrollPane;
    private JTable table;

    public PersonJFrame(String title) throws FileNotFoundException, IOException
    {
        super(title);
        setSize(400, 200);
        setVisible(true);

        table = new JTable();
        scrollPane = new JScrollPane(table);
        getContentPane().add(scrollPane, BorderLayout.CENTER);

        ArrayList<Person> personlist = FileLoading();
        table.setModel(new PersonTable(personlist));
    }

    public static ArrayList<Person> FileLoading() throws FileNotFoundException, IOException
    {
        ArrayList<Person> personinfo = new ArrayList();
        BufferedReader file = new BufferedReader(new FileReader("src/Person.txt"));
        {
            String line = file.readLine();
            String[] temp = line.split(",");

            while((line = file.readLine()) != null)
            {
                temp = line.split(",");
                personinfo.add(new Person(temp[0], temp[1], Integer.parseInt(temp[2]), temp[3]));
            }
        }
        return personinfo;
    }

}

package sample;
        import java.util.ArrayList;
        import javax.swing.table.AbstractTableModel;

public class PersonTable extends AbstractTableModel
{
    private final String[] headers = {"Name", "Surname", "Age", "Gender"};
    private final ArrayList<Person> person;

    public PersonTable(ArrayList<Person> person)
    { this.person = person; }

    @Override
    public int getColumnCount()
    { return headers.length; }

    @Override
    public String getColumnName(int header)
    { return headers[header]; }

    @Override
    public int getRowCount()
    { return person.size(); }

    @Override
    public Object getValueAt(int row, int column)
    {
        Person p = person.get(row);
        if(column==0){ return p.getName(); }
        else if(column ==1) { return p.getSurname(); }
        else if(column ==2) { return p.getAge(); }
        else if(column ==3) { return p.getGender(); }
        else { return null; }
    }

}

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question