• Post Reply Bookmark Topic Watch Topic
  • New Topic

can't select the very last row in a jtable

 
Robert Ingmarsson
Greenhorn
Posts: 27
Java Netbeans IDE PHP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello world!

I have encountered a very tricky spot in a project that i am working on. I must have a JTable to
programmatically select the very last row in an AbstractTableModel. I have tried every thing but
nothing works so far.

The problem is is that the JTable only scrolls down to the second last table row in the model?
What am i missing? I have worked so hard on a project that must be done by monday and i
am a "little" worried. Any tips or links posted here are greatly and very appreciated.

Thanks a lot in advance!




What happens is that the only row that gets selected is the second last row of the total rows
iny AbstractTableModel object, "CableTableModel"

What am i missing?



 
Rob Camick
Ranch Hand
Posts: 2699
10
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why are you invoking the getRowCount() method twice? You only need one variable.

Why are you looping through all the rows in the table? If you want to scroll to the last row then just get the rectangle for the last row.

Use the scrollRectToVisible(..) method on the JTable, not the viewport.

If none of those suggestions help, then post a proper SSCCE. That is create a JFrame with a JTable and an "Add Row" button. The AddRow button will add a new row to the TableModel and then scroll to that row. Once you get a simple SSCCE working you apply the knowledge to your real application.
 
Robert Ingmarsson
Greenhorn
Posts: 27
Java Netbeans IDE PHP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Best Mr Camick!

Thank you so much for the feedback. I did a few erors with the code posted here recently.
I had been up until a very shameful clock hour so i must have missed out a lot of things.
But anyway now it is working. I created a class that implements TableModelListener and
registered it on my AbstractTableModel object and in its tableChanged method i used getLastRow in
a Runnable/Swingutilities method and called invokeLater afterward. My JTable now selects
the very last row after an insertion based event.

Kind regards,
Robert.



Merry easter to all!

 
Robert Ingmarsson
Greenhorn
Posts: 27
Java Netbeans IDE PHP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Robert Ingmarsson wrote:Hello world!

I have encountered a very tricky spot in a project that i am working on. I must have a JTable to
programmatically select the very last row in an AbstractTableModel. I have tried every thing but
nothing works so far.

The problem is is that the JTable only scrolls down to the second last table row in the model?
What am i missing? I have worked so hard on a project that must be done by monday and i
am a "little" worried. Any tips or links posted here are greatly and very appreciated.

Thanks a lot in advance!




What happens is that the only row that gets selected is the second last row of the total rows
iny AbstractTableModel object, "CableTableModel"

What am i missing?



 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!