通过JTextField值对JTable行进行过滤 [英] JTable Row filtering by JTextField value

查看:265
本文介绍了通过JTextField值对JTable行进行过滤的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在JTextfield上输入了一个名称,但是我的表没有过滤任何东西!

I enter a name on my JTextfield , But my table don't filter any thing!

我的代码:

public class UserPage_Admin extends JFrame {

JTable table;
UserModel model;
public UserPage_Admin() {

    model = new UserModel(...);
    TableRowSorter sorter = new TableRowSorter<TableModel>(model);
    table = new JTable(model);
    table.setRowSorter(sorter);

    add(new JScrollPane(table), BorderLayout.CENTER);
    add(panelForm(), BorderLayout.PAGE_START);

    RowFilter<UserModel, Object> rf = null;
    try {
        rf = RowFilter.regexFilter(filterTF.getText(), 0);
    } catch (PatternSyntaxException pse) {
        return;
    }
    sorter.setRowFilter(rf);

    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setSize(850, 600);
    setVisible(true);
}

推荐答案

您正在UserPage_Admin()构造函数中调用RowFilter.regexFilter(filterTF.getText(), 0);.如何从filterTF读取文本.我认为您应该从分配给JButton的动作事件监听器中调用它,该提交器将在提交(单击)文本时被调用,如下所示:

you are calling RowFilter.regexFilter(filterTF.getText(), 0); in UserPage_Admin() constructor. How it supposed to read the text from the filterTF. I think you should call it from an Action Event Listener assigned to a JButton which will be called upon submitting(clicking) the text as follows:

submitButton.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, 0));
        }
      }
    }); 

如果要使用根据用户键类型进行过滤"事件,请添加键侦听器到您要输入过滤字符串的文本字段.

If you want to use Filtering upon user key type event, add key listener to the text field you are taking input filter-string.

filterTxtFeild.addKeyListener(new KeyAdapter() {
            public void keykeyReleased(KeyEvent evt) {
              // on each key type event filter. 
             // put your filter code as submit button
            }
        });

但是,如下面的注释中所建议,要使用Swing文本组件,应该使用文档代表其内容.文档内容以任何方式更改时,都会发生文档事件.添加文档侦听器,如下所示:

However, as it is suggested in the comments below, to work with Swing Text Component, one should have used the Document.addDocumentListener(DocumentListener). A Swing text component uses a Document to represent its content. Document events occur when the content of a document changes in any way. Add the document listener as follows:

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

      @Override
      public void insertUpdate(DocumentEvent e) {
        // put your filter code here upon data insertion
      }

      @Override
      public void removeUpdate(DocumentEvent e) 
      { 
            //put your filter code here upon data removal
      }

      @Override
      public void changedUpdate(DocumentEvent e) {}
    });

为什么最好使用DocumentListener?

如果我们想验证数据源中的输入,请在过滤数据时使用KeyEvent,您会发现它并不反映用户的击键,并且输入事件在被数据源处理之前已发送给侦听器.假设当我们要输入user name时,有人输入了"$%^&"之类的文本.在此类无效输入上,即使未对数据源进行有效更改,仍会触发KeyEvent.但是,仅当对数据源进行了有效更改时,才会通知DocumentListeners.数据输入组件会产生事件,其中validator可以监听asynchronously,但永远不要DocumentListener内部修改文本组件的内容.如果这样做,程序可能会死锁.

If we want validation of input in the data source, using KeyEvent while filtering the data you’ll find it does not reflect the user’s keystroke and input events are sent to the listeners before they are processed by the data source. suppose that when we want an user name to be entered, someone input a text like "$%^&". On such invalid input, KeyEvent will still be fired even though no valid changes has been made to data source. But, DocumentListeners are notified only when a valid changes has been made to the data source. Data entry components produce events in which a validator can listen for asynchronously, one should never modify the contents of a text component from within a DocumentListener. If we do so, the program will likely deadlock.

这篇关于通过JTextField值对JTable行进行过滤的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆