在jTable中选择一行会产生错误 [英] Selecting a row in jTable generates error

查看:219
本文介绍了在jTable中选择一行会产生错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在这里尝试做的是,当我在表中选择一行时,在加载数据后,它应该在文本字段(selectedRowTF)中显示所选行的值.但是,一旦我单击jButton,就会生成以下错误-

What i'm trying do here is that when i select a row in my table , after loading data , it should display the selected row's value in a text field (selectedRowTF). But as soon as i click on the jButton the following error generates -

java.lang.ArrayIndexOutOfBoundsException:-1

由于错误甚至发生在数据加载之前,因此我什至没有机会选择行.

Since the error occurs even before the data is loaded , it leaves no chance for me to even select a row.

您还认为我正在使用正确的代码来获取行的值吗?

Also do you think I'm using the correct code to get the row's value?

堆栈跟踪

DefaultTableModel model;
model=(DefaultTableModel)tbl.getModel();
try {
    Class.forName("java.sql.Driver");
    Connection con= DriverManager.getConnection("jdbc:mysql://localhost:3306/divign","root","password");
    Statement stmt=con.createStatement();

    int row=tbl.getSelectedRow();
    String rowSelected=(tbl.getModel().getValueAt(row, 0).toString());

    String query="SELECT * FROM CUSTOMER WHERE CUSTOMER_ID = '"+customerIdTF.getText()+"' ;";

    ResultSet rs=stmt.executeQuery(query);
    if(rs.next()) {
        model.addRow (new Object[ ] {
            rs.getInt(1),rs.getString(2),rs.getString(3)
            });
        selectedRowTF.setText(""+rowSelected);  
    }
    rs.close();
    stmt.close();
    con.close();
}
catch(Exception e){
    JOptionPane.showMessageDialog(null,e.toString());
}

推荐答案

您的情况是,当您调用tbl.getSelectedRow()时,它返回了-1.这是一个前哨值,指示未选择任何行.当鼠标单击该行或键盘将所选内容移至该行时,将选中该行.当未选择任何行时,您将需要处理这种情况.我不知道您的要求和目标,因此我无法为您提供建议.如果希望在选择更改时收到通知并对其做出反应,则可以向JTable注册选择侦听器. (请参见 getSelectionModel addListSelectionListener ListSelectionListener )

Your situation is that when you called tbl.getSelectedRow(), it returned -1. That is a sentinel value to indicate that no row is selected. A row is selected when the mouse clicks on it, or when the keyboard moves the selection to it. You will need to handle the situation when no row is selected. I don't know your requirements and goals, so I can't advise on how to do that. You can register a selection listener with the JTable if you want to be notified when the selection changes and react to it. (See getSelectionModel, addListSelectionListener, and ListSelectionListener)

如果未在JTable上启用排序或过滤功能,则可能会不做任何处理,但是值得一提的是, view (JTable)中的行索引不是在模型(DefaultTableModel)中具有相同的索引.使用 convertRowIndexToModel 方法从视图的索引(由getSelectedRow()报告)转换为模型的索引(由getValueAt()要求).

You may get away without handling it if you didn't enable sorting or filtering on your JTable, however it is worth knowing that the row indexes in the view (JTable) are not the same indexes in the model (DefaultTableModel). Use the convertRowIndexToModel method to convert from the view's index (as reported by getSelectedRow()) to the model's index (required by getValueAt()).

您发布的代码还包含SQL注入问题.我建议研究该主题,并使用 setString() .

The code you posted also contains SQL Injection issues. I recommend researching that topic, and fix it using a PreparedStatement and setString().

这篇关于在jTable中选择一行会产生错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆