1 package org.objectstyle.cayenne.modeler.util; 2 3 import java.awt.Rectangle ; 4 5 import javax.swing.JTable ; 6 import javax.swing.JViewport ; 7 8 12 public class UIUtil { 13 14 17 public static void scrollToSelectedRow(JTable table) { 18 int row = table.getSelectedRow(); 19 if (row >= 0) { 20 scroll(table, row, 0); 21 } 22 } 23 24 28 public static void scroll(JTable table, int rowIndex, int vColIndex) { 29 if (!(table.getParent() instanceof JViewport )) { 30 return; 31 } 32 33 JViewport viewport = (JViewport ) table.getParent(); 34 Rectangle rect = table.getCellRect(rowIndex, vColIndex, true); 35 Rectangle viewRect = viewport.getViewRect(); 36 37 if (viewRect.intersects(rect)) { 38 return; 39 } 40 41 rect.setLocation(rect.x - viewRect.x, rect.y - viewRect.y); 45 46 int centerX = (viewRect.width - rect.width) / 2; 48 int centerY = (viewRect.height - rect.height) / 2; 49 50 if (rect.x < centerX) { 53 centerX = -centerX; 54 } 55 if (rect.y < centerY) { 56 centerY = -centerY; 57 } 58 rect.translate(centerX, centerY); 59 60 viewport.scrollRectToVisible(rect); 62 } 63 } 64 | Popular Tags |