Rectangle (JComponent component, int row) { if (component instanceof JTable) { Rectangle rectangle = ((JTable)component).getCellRect(row, 0, true); rectangle.width = component.getWidth(); return rectangle; } else if (component instanceof JList) { return ((JList<?>)component).getCellBounds(row, row); } else if (component instanceof JTree) { return ((JTree)component).getRowBounds(row); } else { throw new IllegalArgumentException("Unsupported component: " + component); } }