Reputation: 123
I have a JTable with 60 rows (Fixed).
Everytime an event happens the cell inside that row is updated.
The event is trigged when a NFC Reader writes data on a card. That happens every 2 or 3 seconds.
If the card is written correctly or not the jTable is updated with the result (WRITE, ERRO).
The function to move the JTable to the position only receives the row number.
The scroll is now moved to that row position.
Problem is: After a few row updates the Header of even other rows appears on top of the selected focus row...
Like the Image below.
This are the codes that I already used. They all do the same...
//First code
tblViewCsv.requestFocus();
tblViewCsv.changeSelection(rownumber, 0, false, false);
//2nd code
tblViewCsv.getSelectionModel().setSelectionInterval(rownumber, rownumber);
tblViewCsv.scrollRectToVisible(new Rectangle(tblViewCsv.getCellRect(rownumber, 0, true)));
//3rd code
JViewport viewport = (JViewport) tblViewCsv.getParent();
Rectangle rect = tblViewCsv.getCellRect(rownumber, 0, true);
Rectangle viewRect = viewport.getViewRect();
rect.setLocation(rect.x - viewRect.x, rect.y - viewRect.y);
int centerX = (viewRect.width - rect.width) / 2;
int centerY = (viewRect.height - rect.height) / 2;
if (rect.x < centerX) {
centerX = -centerX;
}
if (rect.y < centerY) {
centerY = -centerY;
}
rect.translate(centerX, centerY);
viewport.scrollRectToVisible(rect);
This is the only method that scrolls to the selected.
This only receives an indicator of row number.
The row number is correct in all cases.
public void scrollPanel(int line) {
tblViewCsv.changeSelection(line, 0, false, false);
tblViewCsv.getSelectionModel().setSelectionInterval(line, line);
tblViewCsv.scrollRectToVisible(new Rectangle(tblViewCsv.getCellRect(line, 0, true)));
}
Any thoughts? Thanks
Upvotes: 1
Views: 150
Reputation: 324207
The event is trigged when a NFC Reader writes data on a card. That happens every 2 or 3 seconds.
So I'm guessing this is a separate Thread running in the background.
So you need to:
table.scrollRectToVisible(..)
since you want to scroll the table in the viewport of the scrollpane.SwingUtilities.invokeLater(...)
. Read the Swing tutorial on Concurrency for more information about the EDT.Upvotes: 3
Reputation: 56
By looking at the code you provided, i think you have to check:
if( rownumber>=0 ) {
//Your code
}
Maybe is a rendering problem caused by a rownumber = -1.
Upvotes: 0