【发布时间】:2015-07-08 05:05:03
【问题描述】:
我是一个初学者,我有一个 JTable,我想用一个结果集填充它,每次用户在 TextField 中输入搜索关键字然后单击搜索按钮时,该结果集都会发生变化。我现在搜索了一周,但我仍然不知道如何在每次单击搜索按钮后将 AbstratTableModel 与我的结果集一起使用来显示和刷新 JTable。
这是完整的代码:
view.CatalogueSWING.java
public class CatalogueSWING extends JFrame{
JLabel jLabelMC = new JLabel("Key Word");
JTextField jTextFieldMC = new JTextField(20); // The textfield that contains the search keyword
JButton jButtonSearch = new JButton("Search");
CatalogueBusiness ca; // the business class which contains the "SearchByKeyWord method"
JTable table;
JPanel pCenter;
//---
public CatalogueSWING() {
ca = new CatalogueBusiness();
JPanel pNorth = new JPanel();
pNorth.setLayout(new FlowLayout());
pNorth.add(jLabelMC);
pNorth.add(jTextFieldMC);
pNorth.add(jButtonSearch);
pCenter = new JPanel();
pCenter.setLayout(new BorderLayout());
pCenter.add(table);
this.setLayout(new BorderLayout());
this.add(pCenter, BorderLayout.CENTER);
this.setSize(900, 500);
pCenter.add(new JScrollPane(table));
this.add(pNorth, BorderLayout.NORTH);
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
this.setVisible(true);
jButtonSearch.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e)
{
String kw = jTextFieldMC.getText();
table = new JTable(ca.SearchByKeyWord(kw)); // SearchByKeyWord(String kw) is the method in my CatalogueBusiness class in another package.
pCenter.add(table);
System.out.println("You clicked the button");
}
});
}
这是商务舱:
business.CatalogueBusiness.java:
@Override
public List<Product> SearchByKeyWord(String kw) {
List<Product> listProducts = new ArrayList<Product>();
try {
Class.forName("com.mysql.jdbc.Driver");
Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/DB_TPJDBC","root","password");
PreparedStatement ps = conn.prepareStatement("select * from PRODUCTS where NAME_PROD like ?");
ps.setString(1, "%"+kw+"%");
ResultSet rs = ps.executeQuery();
while (rs.next()) {
Productp = new Produit();
p.setIdProduct(rs.getInt("ID_PROD"));
p.setNomProduct(rs.getString("NAME_PROD"));
p.setPrice(rs.getDouble("PRICE"));
p.setQuantite(rs.getInt("QUANTITY"));
listProducts.add(p);
}
//ps.close();
//conn.close();
} catch (Exception e) {
e.printStackTrace();
}
return listProducts;
}
这是产品类:
business.Product.java:
import java.io.Serializable;
public class Product implements Serializable {
private int idProduct;
private String nomProduct;
private double price;
private int quantity;
public Produit(String nomProduct, double price, int quantity) {
super();
this.nomProduct = nomProduct;
this.price = price;
this.quantity = quantity;
}
public Product() {
super();
}
// getters & setter ...
// ...
最后是 AbsractTableModel
business.ProductModel.java
public class ProduitModel extends AbstractTableModel{
@Override
public int getColumnCount() {
//....
}
@Override
public int getRowCount() {
//...
}
@Override
public Object getValueAt(int rowIndex, int columnIndex) {
//...
}
@Override
public String getColumnName(int column) {
//...
}
我停在这里,我不知道如何在我的情况下使用 AbstractTableModel,我搜索了很多,但我仍然不知道如何在每次单击“搜索按钮”时将它与我的结果集一起使用以不断更新 JTable在搜索文本字段中添加一些关键字。
提前谢谢你。
【问题讨论】:
标签: java swing jtable resultset abstracttablemodel