简体   繁体   English

我可以在不使用 rs2xml.jar 的情况下为 jTable 创建一个方法吗?

[英]Can i create a method for jTable without using rs2xml.jar?

I am new to programming and I have coded a program using netbeans 8.1 .我是编程新手,我使用 netbeans 8.1 编写了一个程序。 My program is for displaying all the relevant items to be searched in a jTable.我的程序用于显示要在 jTable 中搜索的所有相关项目。 And then we can select the specific item in another jTable.然后我们可以选择另一个 jTable 中的特定项。 I used rs2xml to create a method for jTable and it works fine.我使用 rs2xml 为 jTable 创建了一个方法,它工作正常。 But after i use it, my searching have gone wrong.但是在我使用它之后,我的搜索出错了。 It wont display the correct items for the search.它不会显示正确的搜索项目。 when i remove rs2xml.jar from library search works correctly, but when i select the item, it won't display in jTable.当我从库搜索中删除 rs2xml.jar 时工作正常,但是当我选择该项目时,它不会显示在 jTable 中。 I can't figure this out.我想不通。

here is the code for the search item:这是搜索项的代码:

     private void txtSearchKeyReleased(java.awt.event.KeyEvent evt) {                                      

    try {

        @SuppressWarnings("LocalVariableHidesMemberVariable")
        ResultSet rs = oilmart.getConnection().createStatement().executeQuery("SELECT * FROM stock WHERE Item_Name LIKE '%" + txtSearch.getText() + "%'");
        if (rs.next()) {

            billinfo();
            txtPlace.setText(rs.getString("Place"));
        } else {

            JOptionPane.showMessageDialog(this, "Result not found", null, JOptionPane.ERROR_MESSAGE, null);

        }

    } catch (SQLException | HeadlessException e) {
    }

    // TODO add your handling code here:
}    

And this is the table method i created using rs2xml.jar:这是我使用 rs2xml.jar 创建的表方法:

    public void billinfo() {

    DefaultTableModel dtm = (DefaultTableModel) tblBillinfo.getModel();
    dtm.setRowCount(0);

    try {
        @SuppressWarnings("LocalVariableHidesMemberVariable")
        ResultSet rs = oilmart.getConnection().createStatement().executeQuery("SELECT * FROM stock WHERE Item_Name LIKE '%" + txtSearch.getText() + "%'");

        while (rs.next()) {

            Vector v = new Vector();
            v.add(rs.getString("Item_No"));
            v.add(rs.getString("Item_Name"));
            v.add(rs.getString("Qty"));
            v.add(rs.getString("Price_per_Qty"));
            v.add(rs.getString("Place"));

            buy_price = Integer.parseInt(rs.getString("Price_per_Qty"));

            dtm.addRow(v);
            tblBillinfo.setModel(DbUtils.resultSetToTableModel(rs));
        }

    } catch (Exception e) {
    }

}

And this is for selecting the specific item:这是用于选择特定项目:

     private void tblBillinfoMouseClicked(java.awt.event.MouseEvent evt) {                                         
    x++;

    int r = tblBillinfo.getSelectedRow();

    String no = tblBillinfo.getValueAt(r, 0).toString();
    String name = tblBillinfo.getValueAt(r, 1).toString();
    String buy = tblBillinfo.getValueAt(r, 3).toString();

    buy_price = (int) tblBillinfo.getValueAt(r, 3);

    String plc = tblBillinfo.getValueAt(r, 4).toString();

    tblBill.setValueAt(no, x, 0);
    tblBill.setValueAt(name, x, 1);
    tblBill.setValueAt(buy, x, 2);
    txtPlace.setText(plc);


}                                        

Please help me to figure this out.请帮我解决这个问题。 Thanks.谢谢。

You have code (which appears to be correct) to add each row of data to the TableModel:您有将每行数据添加到 TableModel 的代码(似乎是正确的):

dtm.addRow(v);

But then your very next statement replaces the first row with the remaining rows of data in the ResultSet:但是接下来的语句将第一行替换为 ResultSet 中剩余的数据行:

tblBillinfo.setModel(DbUtils.resultSetToTableModel(rs));

The result is that your TableModel will be missing the first row of data.结果是您的 TableModel 将丢失第一行数据。

Just get rid of the above statement.只要摆脱上面的说法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM