现在我有一个使用 KeyReleased 事件实现的搜索 textField,当我开始输入例如“Andrew”中的“An”时,它没有找到/更新 jTable,并且在我完成准确输入(caseSensitive)我想要的名称之后寻找。
所以,我想要的是从这个站点实现过滤方法,但我有很大的问题。在执行以下实现并删除“旧”KeyReleased 事件后,当我在文本字段“txt_search”中键入内容时,jTable 没有任何反应。
我的IDE(NetBeans)生成的表代码是(从自定义代码面板复制):
Table_Employee = new javax.swing.JTable();
Table_Employee.setModel(new javax.swing.table.DefaultTableModel(
new Object [][] {
{null, null, null, null},
{null, null, null, null},
{null, null, null, null},
{null, null, null, null}
},
new String [] {
"Title 1", "Title 2", "Title 3", "Title 4"
}
)
);
Table_Employee.setToolTipText("Employee info table");
Table_Employee.setCursor(new java.awt.Cursor(java.awt.Cursor.DEFAULT_CURSOR));
Table_Employee.getTableHeader().setReorderingAllowed(false);
Table_Employee.addMouseListener(new java.awt.event.MouseAdapter() {
public void mouseClicked(java.awt.event.MouseEvent evt) {
Table_EmployeeMouseClicked(evt);
}
});
Table_Employee.addKeyListener(new java.awt.event.KeyAdapter() {
public void keyPressed(java.awt.event.KeyEvent evt) {
Table_EmployeeKeyPressed(evt);
}
});
jScrollPane1.setViewportView(Table_Employee);
我在我的面板类中添加了这个,其中 table 是:
import java.awt.Toolkit;
import java.awt.event.WindowEvent;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import javax.swing.JOptionPane;
import net.proteanit.sql.DbUtils;
import java.awt.event.*;
import java.util.Calendar;
import java.util.GregorianCalendar;
import java.text.*;
import javax.swing.*;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableRowSorter;
public class Employee_info extends javax.swing.JFrame {
Connection conn=null;
ResultSet rs=null;
PreparedStatement pst=null;
private TableRowSorter<DefaultTableModel> sorter;
/**
* Creates new form Employee_info
*/
public Employee_info() {
initComponents();
conn=javaconnect.ConnecrDb();
Update_table();
Fillcombo();
currentDate();
Table_Employee.setAutoCreateRowSorter(true);
Table_Employee.setFillsViewportHeight(true);
Table_Employee.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
DefaultTableModel model = new DefaultTableModel();
sorter = new TableRowSorter<DefaultTableModel>(model);
Table_Employee = new JTable(model);
txt_search = new JTextField();
//Whenever filterText changes, invoke newFilter.
txt_search.getDocument().addDocumentListener(
new DocumentListener() {
public void changedUpdate(DocumentEvent e) {
newFilter();
}
public void insertUpdate(DocumentEvent e) {
newFilter();
}
public void removeUpdate(DocumentEvent e) {
newFilter();
}
});
}
private void newFilter() {
RowFilter<DefaultTableModel, Object> rf = null;
//If current expression doesn't parse, don't update.
try {
rf = RowFilter.regexFilter(txt_search.getText(), 0);
} catch (java.util.regex.PatternSyntaxException e) {
return;
}
sorter.setRowFilter(rf);
}
I really need some help, please! This is my last chance, because I really made the google search solutions, and nothing changed.
With respect, Andrew