有三个textFields和搜索按钮:
package suncomputer;
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Vector;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.RowFilter;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableModel;
import javax.swing.table.TableRowSorter;
public class ViewStock extends javax.swing.JFrame {
Vector originalTableModel;
DocumentListener documentListener;
DefaultTableModel model = new DefaultTableModel();
JTable table=null;
Vector<Product> allStock=null;
boolean flag=true;
JLabel labelProductName=new JLabel("Product Name");
JComboBox listProduct=new JComboBox();
JLabel labelUnitPrice=new JLabel("Unit Price");
JTextField textUnitPrice=new JTextField(20);
JLabel labelQty=new JLabel("Qty");
JTextField textQty=new JTextField(20);
JCheckBox searchOnType=new JCheckBox("Search On Type");
JButton button_search=new JButton("Search");
public ViewStock() {
JPanel newPanelSearch = new JPanel(new GridBagLayout());
GridBagConstraints constraints_search = new GridBagConstraints();
constraints_search.anchor = GridBagConstraints.WEST;
constraints_search.insets = new Insets(10, 10, 10, 10);
// add components to the panel
constraints_search.gridx = 0;
constraints_search.gridy = 0;
newPanelSearch.add(labelProductName, constraints_search);
constraints_search.gridx = 1;
newPanelSearch.add(listProduct, constraints_search);
constraints_search.gridx = 0;
constraints_search.gridy = 1;
newPanelSearch.add(labelUnitPrice, constraints_search);
constraints_search.gridx = 1;
newPanelSearch.add(textUnitPrice, constraints_search);
constraints_search.gridx = 0;
constraints_search.gridy = 2;
newPanelSearch.add(labelQty, constraints_search);
constraints_search.gridx = 1;
newPanelSearch.add(textQty, constraints_search);
constraints_search.gridx=3;
newPanelSearch.add(button_search,constraints_search);
constraints_search.gridx=8;
newPanelSearch.add(searchOnType,constraints_search);
newPanelSearch.setBorder(BorderFactory.createTitledBorder(
BorderFactory.createEtchedBorder(), "Product Search"));
// add the panel to this frame
this.add(newPanelSearch,BorderLayout.NORTH);
button_search.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
searchTableContents(textUnitPrice.getText());
}
});
//initComponents();
this.setTitle("Stock Details");
commonMethod comonmethod=new commonMethod();
allStock= comonmethod.viewStock();
if(allStock != null ){
JPanel newPanel = new JPanel(new GridBagLayout());
GridBagConstraints constraints = new GridBagConstraints();
constraints.anchor = GridBagConstraints.WEST;
constraints.insets = new Insets(10, 10, 10, 10);
if(flag){
model.addColumn("Product Id");
model.addColumn("Company Name");
model.addColumn("Product Name");
model.addColumn("Qty");
model.addColumn("Rate");
table = new JTable(model);
flag=false;
}
model=(DefaultTableModel)table.getModel();
System.out.println("list size"+allStock.size());
for(int i=0;i<allStock.size();i++){
Product product=new Product();
product=allStock.get(i);
Vector<Object> customer=new Vector<Object>();
customer.add(product.getProductid());
customer.add(product.getCompanyName());
listProduct.addItem(product.getProductName());
customer.add(product.getProductName());
customer.add(product.getQty());
customer.add(product.getRate());
model.addRow(customer);
}
JScrollPane scrollpane=new JScrollPane(table);
this.add(newPanel,BorderLayout.SOUTH);
this.add(scrollpane,BorderLayout.CENTER);
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
this.setBounds(0,0,screenSize.width, screenSize.height);
this.setVisible(true);
this.pack();
}else
{
JOptionPane.showMessageDialog(rootPane,"Stock Not Available");
}
searchOnType.setText("Search on Type");
searchOnType.addItemListener(new java.awt.event.ItemListener() {
public void itemStateChanged(java.awt.event.ItemEvent evt) {
searchOnTypeItemStateChanged(evt);
}
});
originalTableModel = (Vector) ((DefaultTableModel) table.getModel()).getDataVector().clone();
addDocumentListener();
}
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;
}
}
}
}
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(textUnitPrice.getText());
}
};
searchOnType.setSelected(true);
}
private void searchOnTypeItemStateChanged(java.awt.event.ItemEvent evt) {
if (searchOnType.isSelected()) {
textUnitPrice.getDocument().addDocumentListener(documentListener);
} else {
textUnitPrice.getDocument().addDocumentListener(null);
}
}
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 400, Short.MAX_VALUE)
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 300, Short.MAX_VALUE)
);
pack();
}// </editor-fold>
}
如果我选择任何产品,那么表格应仅显示此产品记录。
如果我输入Unitprice,则只显示此记录。
如果我只输入数量,则只显示此数据记录。
如果我输入productname和qty,则应显示此productname和qty记录。
如果我输入unitprice和qty,则应显示此unitprice和qty记录。