jTable函数getSelectedRow(), getSelectedColumn()返回-1。

时间:2022-03-15 11:58:11

I am new in Java programming. I need to get the indices of selected column and row. I am getting -1 as selected indices for both the column and row. I have searched for a solution but didn't find anything satisfactory.

我是Java编程新手。我需要得到选定的列和行的索引。我得到-1作为列和行的选定指标。我找了个解决办法,但没有找到令人满意的办法。

My code is following:

我的代码是:

private void deleteProductButtonActionPerformed(java.awt.event.ActionEvent evt) {                                                    
    DefaultTableModel tableModel = (DefaultTableModel) this.productDisplaTable.getModel();
    JTable table = new JTable(tableModel);

    int selectedRowIndex = table.getSelectedRow();
    int selectedColIndex = table.getSelectedColumn();
    System.out.println(selectedRowIndex );
    System.out.println(selectedColIndex);
} 

4 个解决方案

#1


2  

In your code you create a new JTable but you don't add this component to any container. Thus it won't never be visible and no row nor column could ever be selected.

在您的代码中,您创建了一个新的JTable,但是没有将这个组件添加到任何容器中。因此,它不会永远可见,也不会选择任何行或列。

Now, while we can add components dynamically in Swing we tipically place all our components before the top-level container (window) is made visible. In this case you should place the table when you initialize your components (don't forget the scroll pane) and do whatever you need to do when the button is pressed.

现在,当我们可以在Swing中动态地添加组件时,我们会在显示*容器(窗口)之前,将所有的组件轻松地放置起来。在这种情况下,您应该在初始化组件时放置该表(不要忘记滚动窗格),并在按下按钮时做任何需要做的事情。

On the other hand I'm not sure what are you trying to achieve. I mean you already have a table called productDisplaTable. If you want to print the selected row and column in that table then make this little change:

另一方面,我不知道你想要达到什么目的。我的意思是你已经有了一个叫做productdislatable的表。如果您想要打印该表中选定的行和列,那么进行以下小小的更改:

private void deleteProductButtonActionPerformed(java.awt.event.ActionEvent evt) {                                                    
    //DefaultTableModel tableModel = (DefaultTableModel) this.productDisplaTable.getModel();
    //JTable table = new JTable(tableModel);    
    int selectedRowIndex = this.productDisplaTable.getSelectedRow();
    int selectedColIndex = this.productDisplaTable.getSelectedColumn();
    System.out.println(selectedRowIndex );
    System.out.println(selectedColIndex);
} 

#2


3  

You're checking if a row is selected before the JTable has been displayed before the user can even interact with it.

您正在检查是否在JTable显示之前选中了一行,而用户甚至还不能与它交互。

Instead why not have that code in an ActionListener or some other listener so that the user at least has a chance to select something? This suggests that you might have a misunderstanding on how event-driven programming works and need to study the concepts a little bit more.

相反,为什么不在ActionListener或其他监听器中包含这些代码,以便用户至少有机会选择一些东西呢?这表明您可能对事件驱动编程的工作方式有误解,需要进一步研究这些概念。

#3


3  

What makes you think that creating a a new JTable would have any selected rows or columns

为什么您认为创建一个新的JTable会有任何选定的行或列

JTable table = new JTable(tableModel); //???

Try using a table that is actually visible to the user instead

尝试使用用户实际可见的表

#4


0  

Thanks all for taking time to reply. I got the answer I was looking from @dic19's comment. Now I clearly see the mistake I was doing. This was due to my lack of knowledge in Java programming.

谢谢大家花时间回复我。我从@dic19的评论中得到了我正在寻找的答案。现在我清楚地看到了我所犯的错误。这是因为我在Java编程方面缺乏知识。

#1


2  

In your code you create a new JTable but you don't add this component to any container. Thus it won't never be visible and no row nor column could ever be selected.

在您的代码中,您创建了一个新的JTable,但是没有将这个组件添加到任何容器中。因此,它不会永远可见,也不会选择任何行或列。

Now, while we can add components dynamically in Swing we tipically place all our components before the top-level container (window) is made visible. In this case you should place the table when you initialize your components (don't forget the scroll pane) and do whatever you need to do when the button is pressed.

现在,当我们可以在Swing中动态地添加组件时,我们会在显示*容器(窗口)之前,将所有的组件轻松地放置起来。在这种情况下,您应该在初始化组件时放置该表(不要忘记滚动窗格),并在按下按钮时做任何需要做的事情。

On the other hand I'm not sure what are you trying to achieve. I mean you already have a table called productDisplaTable. If you want to print the selected row and column in that table then make this little change:

另一方面,我不知道你想要达到什么目的。我的意思是你已经有了一个叫做productdislatable的表。如果您想要打印该表中选定的行和列,那么进行以下小小的更改:

private void deleteProductButtonActionPerformed(java.awt.event.ActionEvent evt) {                                                    
    //DefaultTableModel tableModel = (DefaultTableModel) this.productDisplaTable.getModel();
    //JTable table = new JTable(tableModel);    
    int selectedRowIndex = this.productDisplaTable.getSelectedRow();
    int selectedColIndex = this.productDisplaTable.getSelectedColumn();
    System.out.println(selectedRowIndex );
    System.out.println(selectedColIndex);
} 

#2


3  

You're checking if a row is selected before the JTable has been displayed before the user can even interact with it.

您正在检查是否在JTable显示之前选中了一行,而用户甚至还不能与它交互。

Instead why not have that code in an ActionListener or some other listener so that the user at least has a chance to select something? This suggests that you might have a misunderstanding on how event-driven programming works and need to study the concepts a little bit more.

相反,为什么不在ActionListener或其他监听器中包含这些代码,以便用户至少有机会选择一些东西呢?这表明您可能对事件驱动编程的工作方式有误解,需要进一步研究这些概念。

#3


3  

What makes you think that creating a a new JTable would have any selected rows or columns

为什么您认为创建一个新的JTable会有任何选定的行或列

JTable table = new JTable(tableModel); //???

Try using a table that is actually visible to the user instead

尝试使用用户实际可见的表

#4


0  

Thanks all for taking time to reply. I got the answer I was looking from @dic19's comment. Now I clearly see the mistake I was doing. This was due to my lack of knowledge in Java programming.

谢谢大家花时间回复我。我从@dic19的评论中得到了我正在寻找的答案。现在我清楚地看到了我所犯的错误。这是因为我在Java编程方面缺乏知识。