package testDemo;
/**
* @author 作者 E-mail:
* @version 创建时间:2019年6月4日 下午4:13:49
* 类说明
*/
import javax.swing.*;
import javax.swing.table.*;
import java.awt.*;
import java.awt.event.*;
public class TestFilter {
public static void main(String args[]) {
JFrame frame = new JFrame("JTable的过滤测试");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Object rows[][] = { { "王明", "中国", 44 }, { "姚明", "中国", 25 },
{ "赵子龙", "西蜀", 1234 }, { "曹操", "北魏", 2112 },
{ "Bill Gates", "美国", 45 }, { "Mike", "英国", 33 } };
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);
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("");
panel.add(filterText, BorderLayout.CENTER);
frame.add(panel, BorderLayout.NORTH);
JButton button = new JButton("过滤");
button.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));
}
}
});
frame.add(button, BorderLayout.SOUTH);
frame.setSize(300, 250);
frame.setVisible(true);
}
}
不同的编写方式
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 MyFirstWork {
//定义界面框架,面板按钮,桌面表格及其数据
JFrame frame =new JFrame("Jtabe 测试文件");
//JScrollPane panel = new JScrollPane();
JTextField text =new JTextField();
JButton button =new JButton("过滤数据");
final JTextField filterText = new JTextField("");
JPanel panel = new JPanel(new BorderLayout());
//定义数据
Object rows[][] = { { "王明", "中国", 44 }, { "姚明", "中国", 25 },
{ "赵子龙", "西蜀", 1234 }, { "曹操", "北魏", 2112 },
{ "Bill Gates", "美国", 45 }, { "Mike", "英国", 33 } };
//定义数据
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);
JScrollPane pane = new JScrollPane(table);
final TableRowSorter<TableModel> sorter = new TableRowSorter<TableModel>(
model);
//主类,运行程序
public static void main(String []args) {
new MyFirstWork();
}
//定义构造器
public MyFirstWork() {
frame.setBounds(400, 550, 600,500);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(pane, BorderLayout.CENTER);
JLabel label = new JLabel("过滤");
panel.add(label, BorderLayout.WEST);
panel.add(filterText, BorderLayout.CENTER);
frame.add(panel, BorderLayout.NORTH);
table.setRowSorter(sorter);
addButton();
}
//添加按钮机器监听事件
private void addButton() {
// TODO 自动生成的方法存根
//定义数据按钮
JButton button = new JButton("过滤");
button.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));
}
}
});
frame.add(button, BorderLayout.SOUTH);
frame.setSize(500, 450);
frame.setVisible(true);
}
}