Skip to Main Content

Java SE (Java Platform, Standard Edition)

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Case-insensitive filtering in JTable

843805Feb 21 2007 — edited Jul 3 2008
Hello,
I have a JDialog with a JTable and a JTextField in it. When you type in the JTextField the JTable values are filtered by RowFilter.regexFilter(text) where text is the String value of the JTextField. The problem I am having is that the filter is case-sensitive, and I need it to be case-insensitive. How could I accomplish this?
Thanks.

I am using JRE1.6
import java.awt.EventQueue;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import javax.swing.GroupLayout;
import javax.swing.JDialog;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.LayoutStyle;
import javax.swing.RowFilter;
import javax.swing.RowSorter;
import javax.swing.SortOrder;
import javax.swing.SwingConstants;
import javax.swing.WindowConstants;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableModel;
import javax.swing.table.TableRowSorter;
import java.util.Vector;
import java.util.regex.PatternSyntaxException;

public class DataDialog extends JDialog {
    private JScrollPane dataScrollPane;
    private JTable dataTable;
    private JTextField searchField;
    private TableRowSorter<TableModel> sorter;

    public DataDialog(Object[][] data) {
        initComponents(data);
    }

    private void initComponents(Object[][] data) {
        setModal(true);
        dataScrollPane = new JScrollPane();
        dataTable = new JTable();
        searchField = new JTextField();
        setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
        dataTable.setModel(new DefaultTableModel(
            data,
            new String[] {"Column 1", "Column 2"}
        ) {
            boolean[] canEdit = {false,false};
            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        dataTable.setSelectionMode(0);
        dataTable.setFocusable(false);
        dataTable.getTableHeader().setReorderingAllowed(false);
        sorter = new TableRowSorter<TableModel>(dataTable.getModel());
        dataTable.setRowSorter(sorter);
        Vector<RowSorter.SortKey> qq = new Vector<RowSorter.SortKey>();
        qq.add(new RowSorter.SortKey(0,SortOrder.ASCENDING));
        sorter.setSortKeys(qq);
		dataScrollPane.setViewportView(dataTable);

        searchField.getDocument().addDocumentListener(new DocumentListener() {
            public void insertUpdate(DocumentEvent evt) {
                searchFieldChangedUpdate(evt);
            }
            public void removeUpdate(DocumentEvent evt) {
                searchFieldChangedUpdate(evt);
            }
            public void changedUpdate(DocumentEvent evt) {
                searchFieldChangedUpdate(evt);
            }
        });
        GroupLayout layout = new GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(GroupLayout.Alignment.LEADING)
            .addGroup(GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(GroupLayout.Alignment.TRAILING)
                    .addComponent(dataScrollPane, GroupLayout.Alignment.LEADING, GroupLayout.DEFAULT_SIZE, 375, Short.MAX_VALUE)
                    .addComponent(searchField, GroupLayout.DEFAULT_SIZE, 334, Short.MAX_VALUE))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(GroupLayout.Alignment.BASELINE)
                    .addComponent(searchField, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(dataScrollPane, GroupLayout.DEFAULT_SIZE, 239, Short.MAX_VALUE)
                .addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
                .addContainerGap())
        );
        pack();
        setLocationRelativeTo(null);
    }

    private void searchFieldChangedUpdate(DocumentEvent evt) {
        try {
            String text = searchField.getText();
            if (text.length() == 0) {
                sorter.setRowFilter(null);
            }
            else
            {
                try{
                    sorter.setRowFilter(RowFilter.regexFilter(text));
                }catch(PatternSyntaxException e) {
                    e.printStackTrace();
                }
            }
        }catch(Exception e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                new DataDialog(new Object[][] {{"a","a"},{"A","A"},{"B","B"},{"b","b"}}).setVisible(true);
            }
        });
    }
}
Message was edited by:
ignignokt84
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Jul 31 2008
Added on Feb 21 2007
5 comments
987 views