在JTable中搜索 - 没有得到正确的输出

时间:2013-11-26 11:21:38

标签: java regex swing search jtable

我在JTable中搜索时遇到问题。我在Stack Overflow上找到了一个方法,但似乎没有用。当我现在搜索时,我什么都没得到。

老实说,我对reg并不是那么好。表达式。

相关代码在我的searchTF keylistener中,但我想向您展示其余部分,以便您了解我想要做的事情。

我需要能够在我的JTable中搜索。

Implement search textField into jTable

这是我试图实现的代码。我可能忘记了什么。

package gui;

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.util.ArrayList;
import java.util.List;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.ListSelectionModel;
import javax.swing.RowFilter;
import javax.swing.SwingConstants;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;
import javax.swing.table.AbstractTableModel;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableModel;
import javax.swing.table.TableRowSorter;

import domains.Member;
import mappers.MemberMapper;

public class ShowMembersGUI {
    private JFrame showMembersFrame;
    private JButton editMemberBtn, closeBtn, removeMemberBtn;
    private JTable table;
    private JScrollPane scrollPane;
    private JPanel p;
    private Object id, fName, lName;
    private int index;
    private final MyTableModel myTableModel;
    private JTextField searchTF;
    private JLabel searchLbl;

    public ShowMembersGUI() {
        MemberMapper mMapper = new MemberMapper();
        List<Member> data = mMapper.selectAllMembers();
        myTableModel = new MyTableModel(data);

        table = new JTable(myTableModel);
        final TableRowSorter<TableModel> sorter = new TableRowSorter<TableModel>(
                myTableModel);

        scrollPane = new JScrollPane(table);
        table.setFillsViewportHeight(true);
        table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
        table.setRowSorter(sorter);

        searchLbl = new JLabel("Søg");
        searchTF = new JTextField(10);
        searchTF.addKeyListener(new KeyListener() {

            @Override
            public void keyPressed(KeyEvent e) {
                if (e.getKeyCode() == KeyEvent.VK_ENTER) {

                    searchTF.getDocument().addDocumentListener(
                            new DocumentListener() {

                                private void searchFieldChangedUpdate(
                                        DocumentEvent evt) {
                                    String text = searchTF.getText();
                                    if (text.length() == 0) {
                                        sorter.setRowFilter(null);
                                        table.clearSelection();
                                    } else {
                                        try {
                                            sorter.setRowFilter(RowFilter
                                                    .regexFilter("(?i)" + text,
                                                            4));
                                            table.clearSelection();

                                        } catch (Exception e) {
                                            e.printStackTrace();
                                        }
                                    }
                                }

                                @Override
                                public void insertUpdate(DocumentEvent evt) {
                                    searchFieldChangedUpdate(evt);
                                }

                                @Override
                                public void removeUpdate(DocumentEvent evt) {
                                    searchFieldChangedUpdate(evt);
                                }

                                @Override
                                public void changedUpdate(DocumentEvent evt) {
                                    searchFieldChangedUpdate(evt);
                                }
                            });
                }
            }

            @Override
            public void keyReleased(KeyEvent arg0) {
                // TODO Auto-generated method stub

            }

            @Override
            public void keyTyped(KeyEvent arg0) {
                // TODO Auto-generated method stub
            }
        });

        editMemberBtn = new JButton("Rediger Medlem");
        editMemberBtn.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                EditMemberGUI editMemberGUI = new EditMemberGUI(
                        ShowMembersGUI.this);
                int row = table.getSelectedRow();
                // int column = table.getSelectedColumn();
                id = (Object) table.getValueAt(row, 0);
                Object fName = (Object) table.getValueAt(row, 1);
                Object lName = (Object) table.getValueAt(row, 2);
                Object address = (Object) table.getValueAt(row, 3);
                Object zipCode = (Object) table.getValueAt(row, 4);
                Object city = (Object) table.getValueAt(row, 5);
                Object email = (Object) table.getValueAt(row, 6);
                Object phoneNo = (Object) table.getValueAt(row, 7);

                index = (Integer) id;
                editMemberGUI.getfNameTF().setText(fName.toString());
                editMemberGUI.getlNameTF().setText(lName.toString());
                editMemberGUI.getAddressTF().setText(address.toString());
                editMemberGUI.getZipCodeTF().setText(zipCode.toString());
                editMemberGUI.getCityTF().setText(city.toString());
                editMemberGUI.getEmailTF().setText(email.toString());
                editMemberGUI.getPhoneNoTF().setText(phoneNo.toString());
                editMemberGUI.showGUI();

            }
        });

        removeMemberBtn = new JButton("Fjern Medlem");
        removeMemberBtn.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                int row = table.getSelectedRow();
                // int column = table.getSelectedColumn();
                id = (Object) table.getValueAt(row, 0);
                fName = (Object) table.getValueAt(row, 1);
                lName = (Object) table.getValueAt(row, 2);

                Object[] options = { "Ja", "Nej" };
                int n = JOptionPane
                        .showOptionDialog(null,
                                "Er du sikker du vil slette medlem?\n\n"
                                        + "Medlemsnr: " + id + "\n\n" + fName
                                        + " " + lName, "Advarsel",
                                JOptionPane.YES_NO_OPTION,
                                JOptionPane.QUESTION_MESSAGE, null, options,
                                options[0]);

                if (n == JOptionPane.YES_OPTION) {
                    MemberMapper mMapper = new MemberMapper();
                    int i = (Integer) id;
                    mMapper.removeMember(i);
                    myTableModel.removeMemberFromTable(row);
                    myTableModel.fireTableDataChanged();
                } else {
                }

            }
        });

        closeBtn = new JButton("Close");
        closeBtn.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent arg0) {
                showMembersFrame.setVisible(false);
            }
        });

        p = new JPanel(new FlowLayout());

        JPanel p1 = new JPanel(new FlowLayout(FlowLayout.LEFT));
        JPanel p2 = new JPanel(new FlowLayout(FlowLayout.RIGHT));

        p1.add(searchLbl);
        p1.add(searchTF);
        p2.add(editMemberBtn);
        p2.add(removeMemberBtn);
        p2.add(closeBtn);

        p.add(p1);
        p.add(p2);

    }

    public void showGUI() {
        showMembersFrame = new JFrame();
        showMembersFrame.setTitle("Vis Medlemmer");
        showMembersFrame.setLayout(new BorderLayout());
        showMembersFrame.setSize(1000, 600);

        showMembersFrame.add(scrollPane, BorderLayout.CENTER);
        showMembersFrame.add(p, BorderLayout.SOUTH);
        // showMembersFrame.pack();
        showMembersFrame.setVisible(true);
    }

    public int getIndex() {
        return index;
    }

    public void setIndex(int index) {
        this.index = index;
    }

    public JTable getTable() {
        return table;
    }

    public void setTable(JTable table) {
        this.table = table;
    }

}

class MyTableModel extends AbstractTableModel {

    private static final long serialVersionUID = 1L;
    private List<Member> members = new ArrayList<Member>();

    public MyTableModel(List<Member> members) {
        this.members = members;
    }

    public void addMember(Member m) {
        members.add(m);
        int row = members.size() - 1;
        fireTableRowsInserted(row, row);
    }

    public void removeMemberFromTable(int row) {
        members.remove(row);
        fireTableRowsDeleted(row, row);
    }

    public void tableUpdate(int row, Member m) {
        if (row >= 0 && row < members.size()) {
            members.set(row, m);
            fireTableRowsUpdated(row, row);
        }
    }

    public List<Member> getMembers() {
        return new ArrayList<Member>(members);
    }

    @Override
    public int getColumnCount() {
        return 8;
    }

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

    @Override
    public Object getValueAt(int rowIndex, int columnIndex) {
        Member m = members.get(rowIndex);

        switch (columnIndex) {
        case 0:
            return m.getMemberId();

        case 1:
            return m.getfName();

        case 2:
            return m.getlName();

        case 3:
            return m.getAddress();

        case 4:
            return m.getZipCode();

        case 5:
            return m.getCity();

        case 6:
            return m.getEmail();

        case 7:
            return m.getPhoneNo();

        default:
            return "";
        }
    }

    public String getColumnName(int column) {
        switch (column) {
        case 0:
            return "MedlemsID";

        case 1:
            return "Fornavn";

        case 2:
            return "Efternavn";

        case 3:
            return "Adresse";

        case 4:
            return "Postnummer";

        case 5:
            return "By";

        case 6:
            return "Email";

        case 7:
            return "Telefonnr";
        default:
            return "";
        }
    }

    public Class<?> getColumnClass(int columnIndex) {
        switch (columnIndex) {
        case 0:
            return String.class;

        case 1:
            return String.class;

        case 2:
            return String.class;

        case 3:
            return String.class;

        case 4:
            return String.class;

        case 5:
            return String.class;

        case 6:
            return String.class;

        case 7:
            return String.class;

        default:
            return String.class;
        }
    }
}

2 个答案:

答案 0 :(得分:1)

下一个设计不良的代码中的问题:

  searchTF.addKeyListener(new KeyListener() {

        @Override
        public void keyPressed(KeyEvent e) {
            if (e.getKeyCode() == KeyEvent.VK_ENTER) {

                searchTF.getDocument().addDocumentListener(
                        new DocumentListener() {

                            private void searchFieldChangedUpdate(
                                    DocumentEvent evt) {
                                String text = searchTF.getText();
                                if (text.length() == 0) {
                                    sorter.setRowFilter(null);
                                } else {
                                    try {
                                        sorter.setRowFilter(RowFilter.regexFilter("(?i)" + text,4));
                                    } catch (Exception e) {
                                        e.printStackTrace();
                                    }
                                }
                            }

                            @Override
                            public void insertUpdate(DocumentEvent evt) {
                                searchFieldChangedUpdate(evt);
                            }

                            @Override
                            public void removeUpdate(DocumentEvent evt) {
                                searchFieldChangedUpdate(evt);
                            }

                            @Override
                            public void changedUpdate(DocumentEvent evt) {
                                searchFieldChangedUpdate(evt);
                            }
                        });
            }
        }

        @Override
        public void keyReleased(KeyEvent arg0) {
            // TODO Auto-generated method stub

        }

        @Override
        public void keyTyped(KeyEvent arg0) {
            // TODO Auto-generated method stub
        }
    });

首先,根据@mKorbel的建议,使用DocumentListener代替KeyListener

其次,正如我所见,您只需在按下ENTER时使用搜索,为此您可以使用keyBindings

并且searchTF.getDocument().addDocumentListener(...)您的班级中的此代码会在您的字段中添加新的DocumentListener,在构建字段时执行一次。

为您的目的使用下一个代码:

    InputMap im = searchTF.getInputMap(JTextField.WHEN_IN_FOCUSED_WINDOW);
    ActionMap am = searchTF.getActionMap();
    im.put(KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 0), "ENTER");
    am.put("ENTER", new AbstractAction() {

        @Override
        public void actionPerformed(ActionEvent arg0) {
             String text = searchTF.getText();
             if (text.isEmpty()) {
                 sorter.setRowFilter(null);
             } else {
                 try {
                     sorter.setRowFilter(RowFilter.regexFilter("(?i)" + text, 4));
                 } catch (Exception e) {
                     e.printStackTrace();
                 }
             }
        }
    });

答案 1 :(得分:0)

我发现了问题所在。

就是这条线。我已将值设置为4. :)

sorter.setRowFilter(RowFilter.regexFilter("(?i)" + text, 4));

我试过这个,但就像他们互相覆盖一样。

sorter.setRowFilter(RowFilter.regexFilter("(?i)" + text, 0));
sorter.setRowFilter(RowFilter.regexFilter("(?i)" + text, 1));
sorter.setRowFilter(RowFilter.regexFilter("(?i)" + text, 2));

但我一次只能搜索一个。复制/粘贴该行以在该参数中写入不同的值时,它仅过滤最后一行。

更新:

sorter.setRowFilter(RowFilter.regexFilter("(?i)" + text, 0, 1, 2));

这很有用。谢谢!