14.64.2 表查询 Filter table by the text in a TextField
2011-09-28 14:25
316 查看
![](http://hi.csdn.net/attachment/201109/28/0_13171912059vvT.gif)
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", "好", 4.40 }, { "H", "Hot", 24.96 }, { "I", "Ivey", 5.45 }, { "J", "Jack", 49.54 }, { "K", "Kids", 280.00 } }; String columns[] = { "符号", "名字", "价格" }; 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);//设置为sorter此方法清除该选择并重置所有可变行高度。 JScrollPane pane = new JScrollPane(table); frame.add(pane, BorderLayout.CENTER); JPanel panel = new JPanel(new BorderLayout()); JLabel label = new JLabel("过滤"); 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("过滤"); button.addActionListener(new ActionListener(){ @Override public void actionPerformed(ActionEvent e) { // TODO Auto-generated method stub String text = filterText.getText();//从文本框里得到的值给text if(text.length() == 0){//如果没有数据 //setRowFilter()设置用于确定哪些行(如果有)应该在视图中隐藏的过滤器。过滤器在排序之前应用。 sorter.setRowFilter(null);//过滤为空,即显示所有行数据 }else{ sorter.setRowFilter(RowFilter.regexFilter(text)); /** * 1.RowFilter 用于从模型中过滤条目,使得这些条目不会在视图中显示。 * 2.regexFilter返回一个 RowFilter,它使用正则表达式确定要包含哪些条目。 * 3.只包含至少有一个匹配值的条目,这里包含了text这个条目 */ } } }); frame.add(button, BorderLayout.SOUTH); frame.setSize(300, 250); frame.setVisible(true); } }
相关文章推荐
- The table name must be enclosed in double quotation marks or sqare bracket while accessing EXCEL by
- The table name must be enclosed in double quotation marks or sqare bracket while accessing EXCEL by
- The table name must be enclosed in double quotation marks or sqare bracket while accessing EXCEL by
- Dispatch时产生了这个异常:does not contain handler parameter named ‘method’ This may be caused by whitespace in the label text
- 百度LBS云搜索时报错 "filter:area is not filteable field, please set property in the cloud-storage
- 错误: In doing so, the sorting sequence - determined by the table key - was destroyed. 之解决
- How to filter the ManyToManyField or ForeignKey in modelForm?
- aotu modify table ' field's value,by ODBC directly is false,but use CDatabase can well and also the header file is needed.
- Django: Filter ModelChoiceField by user(or other args in view)
- The filter pushed in subquery issue in oracle 11G (filter推进子查询)
- Can you refresh a document in the Notes client after changing a rich text field?
- 百度LBS云搜索时报错 "filter:area is not filteable field, please set property in the cloud-storage
- Here's an example that shows how to enumerate through the features of a selection set, returning the value of each field in the attribute table except for the geometry colum.
- query db name and table name by the column name in db server
- How do I cover the “no results” text in UISearchDisplayController's searchResultTableView?
- The ORDER BY clause is invalid in views, inline functions, derived tables, subqueries, and common table expressions, unless TOP or FOR XML is also specified.
- MySQL数据库update更新子查询[Err] 1093 - You can't specify target table 'text' for update in FROM clause
- ORA-02266: unique/primary keys in table referenced by enabled foreign keys
- Question 41: Which of the following operators must be overloaded by function objects in the Standard Template Library?
- php5.3 的问题 “Assigning the return value of new by reference is deprecated in xxxx”