我在netbeans中创建jtable并在jtable中填充数据库中的数据,但是我无法使用jtextfield在jtable中实现搜索选项。任何人都可以指导我。 感谢
答案 0 :(得分:4)
为了在桌面上搜索某些内容,您必须将原始内容存储在某个变量中。在下面的示例中,我将表格模型的初始值存储到向量中。
下面有两个实现。只要在文本字段上输入内容就可以进行搜索,而只有在单击按钮后才进行搜索。
对于第一个,您必须使用documentListener为您做事。
下面的函数将删除表值[如果找不到匹配表将为空]并在所有单元格中搜索字符串,如果找到匹配项,则会将该行添加到表中。
public void searchTableContents(String searchString) {
DefaultTableModel currtableModel = (DefaultTableModel) table.getModel();
//To empty the table before search
currtableModel.setRowCount(0);
//To search for contents from original table content
for (Object rows : originalTableModel) {
Vector rowVector = (Vector) rows;
for (Object column : rowVector) {
if (column.toString().contains(searchString)) {
//content found so adding to table
currtableModel.addRow(rowVector);
break;
}
}
}
}
这是上述
的完整代码import java.util.Vector;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;
import javax.swing.table.DefaultTableModel;
public class TableSearch extends javax.swing.JFrame {
Vector originalTableModel;
DocumentListener documentListener;
public TableSearch() {
initComponents();
setLocationRelativeTo(null);
//backup of original values to check
originalTableModel = (Vector) ((DefaultTableModel) table.getModel()).getDataVector().clone();
//add document listener to jtextfield to search contents as soon as something typed on it
addDocumentListener();
}
private void addDocumentListener() {
documentListener = new DocumentListener() {
public void changedUpdate(DocumentEvent documentEvent) {
search();
}
public void insertUpdate(DocumentEvent documentEvent) {
search();
}
public void removeUpdate(DocumentEvent documentEvent) {
search();
}
private void search() {
searchTableContents(jTextField1.getText());
}
};
searchOnType.setSelected(true);
}
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
jScrollPane1 = new javax.swing.JScrollPane();
table = new javax.swing.JTable();
jPanel1 = new javax.swing.JPanel();
searchOnType = new javax.swing.JCheckBox();
jTextField1 = new javax.swing.JTextField();
searchButton = new javax.swing.JButton();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
table.setModel(new javax.swing.table.DefaultTableModel(
new Object [][] {
{"masd", "asdad", "asdasda", "ert"},
{"gdfg", "name", "test", "dfg"},
{"rrrh", "dfg", "sdfsf", "sdf"},
{"ter", "retg", "wersd", "wer"}
},
new String [] {
"Title 1", "Title 2", "Title 3", "Title 4"
}
));
jScrollPane1.setViewportView(table);
getContentPane().add(jScrollPane1, java.awt.BorderLayout.CENTER);
searchOnType.setText("Search on Type");
searchOnType.addItemListener(new java.awt.event.ItemListener() {
public void itemStateChanged(java.awt.event.ItemEvent evt) {
searchOnTypeItemStateChanged(evt);
}
});
searchButton.setText("Search");
searchButton.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
searchButtonActionPerformed(evt);
}
});
javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
jPanel1.setLayout(jPanel1Layout);
jPanel1Layout.setHorizontalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel1Layout.createSequentialGroup()
.addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, 192, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(searchButton)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(searchOnType)
.addContainerGap())
);
jPanel1Layout.setVerticalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel1Layout.createSequentialGroup()
.addContainerGap()
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(searchButton)
.addComponent(searchOnType))
.addContainerGap())
);
getContentPane().add(jPanel1, java.awt.BorderLayout.NORTH);
pack();
}// </editor-fold>
private void searchButtonActionPerformed(java.awt.event.ActionEvent evt) {
searchTableContents(jTextField1.getText());
}
private void searchOnTypeItemStateChanged(java.awt.event.ItemEvent evt) {
if (searchOnType.isSelected()) {
jTextField1.getDocument().addDocumentListener(documentListener);
} else {
jTextField1.getDocument().addDocumentListener(null);
}
}
public void searchTableContents(String searchString) {
DefaultTableModel currtableModel = (DefaultTableModel) table.getModel();
//To empty the table before search
currtableModel.setRowCount(0);
//To search for contents from original table content
for (Object rows : originalTableModel) {
Vector rowVector = (Vector) rows;
for (Object column : rowVector) {
if (column.toString().contains(searchString)) {
//content found so adding to table
currtableModel.addRow(rowVector);
break;
}
}
}
}
public static void main(String args[]) {
/* Create and display the form */
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new TableSearch().setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.swing.JPanel jPanel1;
private javax.swing.JScrollPane jScrollPane1;
private javax.swing.JTextField jTextField1;
private javax.swing.JButton searchButton;
private javax.swing.JCheckBox searchOnType;
private javax.swing.JTable table;
// End of variables declaration
}
答案 1 :(得分:-1)
我用eclipse中的一列创建jtable,并在jtable中填充数据库中的数据。如果在框架中有两个文本字段,并且 我在Textfield1中输入内容,它将突出显示表中与文本和 在jtable值中选择的那些突出显示行显示在Textfield1中。同样,在Textfield2中也会以相同的形式发生。