Scrolling JTable to a Specified String Pointer

I have a JTable that is inside a JScrollPane. Rows are added to the table at runtime based on events that occur in my application. I want the scoll panel to scroll to the bottom of the table when a new table is added to the table.

For JLists There is [ensureIndexIsVisible][1]() , which makes a specific index in the list be visible. I am looking for the same thing, but for JTable. It looks like I might have to manually move the scroll on the scroll bar, but I decided there should be an easier way.

+41
java swing jtable jscrollpane
May 12 '09 at 14:18
source share
5 answers

See this example: http://www.exampledepot.com/egs/javax.swing.table/Vis.html

update: link is out of date, here is the code (from http://smi-protege.stanford.edu/repos/protege/protege-core/trunk/src/edu/stanford/smi/protege/util/ComponentUtilities.java )

 public static void scrollToVisible(JTable table, int rowIndex, int vColIndex) { if (!(table.getParent() instanceof JViewport)) { return; } JViewport viewport = (JViewport)table.getParent(); // This rectangle is relative to the table where the // northwest corner of cell (0,0) is always (0,0). Rectangle rect = table.getCellRect(rowIndex, vColIndex, true); // The location of the viewport relative to the table Point pt = viewport.getViewPosition(); // Translate the cell location so that it is relative // to the view, assuming the northwest corner of the // view is (0,0) rect.setLocation(rect.x-pt.x, rect.y-pt.y); table.scrollRectToVisible(rect); // Scroll the area into view //viewport.scrollRectToVisible(rect); } 
+32
May 12 '09 at 14:24
source share

It is very simple, JTable also has a scrollRectToVisible method. If you want, you can try something like this to make scrollpane at the bottom if a new entry is added:

 jTable1.getSelectionModel().setSelectionInterval(i, i); jTable1.scrollRectToVisible(new Rectangle(jTable1.getCellRect(i, 0, true))); 

Where i is the last record added.

+63
Jun 03 2018-11-11T00:
source share

JList internally uses scrollRectToVisible and specify the coordinates to scroll through . I think you will have to transcode the same functionality for JTable.

+4
May 12, '09 at 14:27
source share

The first answer works well, but the selected row is located at the bottom of the table. Therefore, I created this modified version:

 private void scrollToVisible(int rowIndex, int vColIndex ) { JTable table = getTablePanel().getTable(); if (!(table.getParent() instanceof JViewport)) { return; } if (table.getRowCount()<1){ return; } JViewport viewport = (JViewport)table.getParent(); // view dimension Dimension dim = viewport.getExtentSize(); // cell dimension Dimension dimOne = new Dimension(0,0); // This rectangle is relative to the table where the // northwest corner of cell (0,0) is always (0,0). Rectangle rect = table.getCellRect(rowIndex, vColIndex, true); Rectangle rectOne; if (rowIndex+1<table.getRowCount()) { if (vColIndex+1<table.getColumnCount()) vColIndex++; rectOne = table.getCellRect(rowIndex+1, vColIndex, true); dimOne.width=rectOne.x-rect.x; dimOne.height=rectOne.y-rect.y; } // '+ veiw dimension - cell dimension' to set first selected row on the top rect.setLocation(rect.x+dim.width-dimOne.width, rect.y+dim.height-dimOne.height); table.scrollRectToVisible(rect); } 

Now the selected row will be located at the top of the table.

+1
Mar 12 '13 at 11:51
source share

I find it much easier to set the position of the viewport instead of scrolling through the table. Below is my code.

 public void scrollCellToView(int rowIndex, int vColIndex) { if (!(this.getParent() instanceof JViewport)) { return; } JViewport viewport = (JViewport) this.getParent(); Rectangle rect = this.getCellRect(rowIndex, vColIndex, true); Rectangle viewRect = viewport.getViewRect(); int x = viewRect.x; int y = viewRect.y; if (rect.x >= viewRect.x && rect.x <= (viewRect.x + viewRect.width - rect.width)){ } else if (rect.x < viewRect.x){ x = rect.x; } else if (rect.x > (viewRect.x + viewRect.width - rect.width)) { x = rect.x - viewRect.width + rect.width; } if (rect.y >= viewRect.y && rect.y <= (viewRect.y + viewRect.height - rect.height)){ } else if (rect.y < viewRect.y){ y = rect.y; } else if (rect.y > (viewRect.y + viewRect.height - rect.height)){ y = rect.y - viewRect.height + rect.height; } viewport.setViewPosition(new Point(x,y)); } 
0
Nov 20 '13 at 22:17
source share



All Articles