JTable filter insensitive case

B

Biagio

Hi o all,

in this sample I don't know how filtering this rows to include upper
and lower case records. If I use the string "micro" the JTable don't
shows the rows:

{"MSFT", "Microsoft", 26.56},
{"SUNW", "Sun Microsystems", 3.86},
{"LIMI", "Linemicro!", 67.69}

but only

{"LIMI", "Linemicro!", 67.69}

import javax.swing.*;
import javax.swing.table.*;
import java.awt.*;
import java.awt.event.*;
import java.util.regex.*;

public class FilterTable {

public static void main(String args[]) {
Runnable runner = new Runnable() {

public void run() {
JFrame frame = new JFrame("Ordenando JTable");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Object rows[][] = {
{"AMZN", "Amazon", 41.28},
{"EBAY", "eBay", 41.57},
{"GOOG", "Google", 388.33},
{"MSFT", "Microsoft", 26.56},
{"NOK", "Nokia Corp", 17.13},
{"ORCL", "Oracle Corp.", 12.52},
{"SUNW", "Sun Microsystems", 3.86},
{"TWX", "Time Warner", 17.66},
{"VOD", "Vodafone Group", 26.02},
{"YHOO", "Yahoo!", 37.69},
{"LIMI", "Linemicro!", 67.69}
};
Object columns[] = {"Sigra", "Nome", "Preco"};
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;
}
};
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("Filtro");
panel.add(label, BorderLayout.WEST);
final JTextField filterText =
new JTextField("micro");
panel.add(filterText, BorderLayout.CENTER);
frame.add(panel, BorderLayout.NORTH);
JButton button = new JButton("Filtro");
button.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent e) {
String text = filterText.getText();
if (text.length() == 0) {
sorter.setRowFilter(null);
} else {
try {
sorter.setRowFilter(
RowFilter.regexFilter(text));
} catch (PatternSyntaxException pse) {
System.err.println("Erro");
}
}
}
});
frame.add(button, BorderLayout.SOUTH);
frame.setSize(300, 250);
frame.setVisible(true);
}
};
EventQueue.invokeLater(runner);
}

}

Any idea?

Thanks and sorry for my english

Biagio
 
Joined
Dec 31, 2009
Messages
6
Reaction score
0
hii,

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

Code:
final TableRowSorter<TableModel> sorter = new TableRowSorter<TableModel>(myTableModel);
        resultCpTable.setRowSorter(sorter);
        filterCpText.getDocument().addDocumentListener(new DocumentListener() {

            private void searchFieldChangedUpdate(DocumentEvent evt) {
                String text = filterCpText.getText();
                if (text.length() == 0) {
                    sorter.setRowFilter(null);
                    resultCpTable.clearSelection();
                    exportCpButton.setEnabled(true);
                } else {
                    try {
                        sorter.setRowFilter(RowFilter.regexFilter("(?i)" + text));
                        resultCpTable.clearSelection();
                        exportCpButton.setEnabled(false);
                        CennePapiere.FilterArrayList.add(new String(RecNo + "-" + text.toString()));
                        RecNo++;
                    } catch (PatternSyntaxException pse) {
                        JOptionPane.showMessageDialog(null, "Bad regex pattern", "Bad regex pattern", JOptionPane.ERROR_MESSAGE);
                    }
                }
            }

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

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

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

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,792
Messages
2,569,639
Members
45,352
Latest member
SherriePet

Latest Threads

Top