为什么我的jtable getSelectedRow()的方法不起作用?

时间:2023-01-27 13:23:17

this is my first question, so help me please. I try to save the value of the method getStelectedRow in a type int variable(row) to next can use the method getValueAt(row,column). My problem is the value of my variable, it's -1, and this means the row is not selected, but I'm selecting a row.

这是我的第一个问题,请帮帮我。我尝试将方法getStelectedRow的值保存在类型int变量(行)中,然后可以使用方法getValueAt(row,column)。我的问题是我的变量的值,它是-1,这意味着没有选择行,但我选择了一行。

The error is the next:

错误是下一个:

Exception in thread "AWT-EventQueue-0" java.lang.ArrayIndexOutOfBoundsException: -1

If need more details just say me. Thanks.

如果需要更多细节,请告诉我。谢谢。

EDIT:

My code is:

我的代码是:

int row = jTablePersonal.getSelectedRow();
String query = "select * from table where id ='"+jTablePersonal.getValueAt(row,0)+"'";

The error point to the variable "row" when I call the method "getValueAt(row,0)"

当我调用方法“getValueAt(row,0)”时,错误指向变量“row”

2 个解决方案

#1


1  

Seems like a newbie problem. Given your explanation

好像是一个新手问题。鉴于你的解释

"My problem is the value of my variable, it's -1, and this means the row is not selected, but I'm selecting a row."

“我的问题是我的变量的值,它是-1,这意味着没有选择行,但我选择了一行。”

You do not have this code inside a listener, you have like it your constructor or something. You want to have the code inside the listener. Something like

你没有在侦听器中使用此代码,你喜欢它的构造函数或其他东西。您希望将代码放在侦听器中。就像是

button.addActionListener(new ActionListener(){
    public void actionPerformed(ActionEvent e) {
        int row = table.getSelectedRow();
        if (row != -1) {
            // do something
        }
    }
});

If you are using the Netbeans GUI Builder tool, you can

如果您使用的是Netbeans GUI Builder工具,则可以

  1. From the design view right click the button and go to Events -> Action -> actionPerformed

    在设计视图中右键单击按钮,然后转到事件 - >操作 - > actionPerformed

  2. Go to you source view and you should see some auto-generated code like

    转到您的源视图,您应该看到一些自动生成的代码

    jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
    
    }
    

    Write the code there.

    在那里写代码。

You should also take some time to read How to write Event Listeners. GUI Programs are event driven, so you need to learn how to respond to these events by registering listeners

您还应该花些时间阅读如何编写事件监听器。 GUI程序是事件驱动的,因此您需要通过注册监听器来学习如何响应这些事件

#2


1  

the problem is about getSelectedRow();

问题是关于getSelectedRow();

getSelectedRow is only work if table is current selected

getSelectedRow仅在当前选择表时才有效

my suggestion is , make temp variable to get last selectedrow to prevent error ,like

我的建议是,使用temp变量来获取最后一个selectrow以防止错误,比如

if(table.getSelectedRow()!=-1)
{
    int lastselected=table.getSelectedRow();
}

#1


1  

Seems like a newbie problem. Given your explanation

好像是一个新手问题。鉴于你的解释

"My problem is the value of my variable, it's -1, and this means the row is not selected, but I'm selecting a row."

“我的问题是我的变量的值,它是-1,这意味着没有选择行,但我选择了一行。”

You do not have this code inside a listener, you have like it your constructor or something. You want to have the code inside the listener. Something like

你没有在侦听器中使用此代码,你喜欢它的构造函数或其他东西。您希望将代码放在侦听器中。就像是

button.addActionListener(new ActionListener(){
    public void actionPerformed(ActionEvent e) {
        int row = table.getSelectedRow();
        if (row != -1) {
            // do something
        }
    }
});

If you are using the Netbeans GUI Builder tool, you can

如果您使用的是Netbeans GUI Builder工具,则可以

  1. From the design view right click the button and go to Events -> Action -> actionPerformed

    在设计视图中右键单击按钮,然后转到事件 - >操作 - > actionPerformed

  2. Go to you source view and you should see some auto-generated code like

    转到您的源视图,您应该看到一些自动生成的代码

    jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
    
    }
    

    Write the code there.

    在那里写代码。

You should also take some time to read How to write Event Listeners. GUI Programs are event driven, so you need to learn how to respond to these events by registering listeners

您还应该花些时间阅读如何编写事件监听器。 GUI程序是事件驱动的,因此您需要通过注册监听器来学习如何响应这些事件

#2


1  

the problem is about getSelectedRow();

问题是关于getSelectedRow();

getSelectedRow is only work if table is current selected

getSelectedRow仅在当前选择表时才有效

my suggestion is , make temp variable to get last selectedrow to prevent error ,like

我的建议是,使用temp变量来获取最后一个selectrow以防止错误,比如

if(table.getSelectedRow()!=-1)
{
    int lastselected=table.getSelectedRow();
}