1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.gui.widgets;
|
---|
3 |
|
---|
4 | import java.awt.Container;
|
---|
5 | import java.awt.Point;
|
---|
6 | import java.awt.Rectangle;
|
---|
7 |
|
---|
8 | import javax.swing.JTable;
|
---|
9 | import javax.swing.JViewport;
|
---|
10 | import javax.swing.table.TableModel;
|
---|
11 |
|
---|
12 | /**
|
---|
13 | * Table offering easier scroll to a given row/column.
|
---|
14 | * @since 11881
|
---|
15 | */
|
---|
16 | public class ScrollableTable extends JTable {
|
---|
17 |
|
---|
18 | /**
|
---|
19 | * Constructs a <code>ScrollableTable</code> that is initialized with
|
---|
20 | * <code>dm</code> as the data model, a default column model,
|
---|
21 | * and a default selection model.
|
---|
22 | *
|
---|
23 | * @param dm the data model for the table
|
---|
24 | * @see #createDefaultColumnModel
|
---|
25 | * @see #createDefaultSelectionModel
|
---|
26 | */
|
---|
27 | public ScrollableTable(TableModel dm) {
|
---|
28 | super(dm);
|
---|
29 | }
|
---|
30 |
|
---|
31 | /**
|
---|
32 | * Scrolls this table to make sure the (row,col) is visible.
|
---|
33 | * @param row row index
|
---|
34 | * @param col column index
|
---|
35 | */
|
---|
36 | public void scrollToVisible(int row, int col) {
|
---|
37 | Container parent = getParent();
|
---|
38 | if (parent instanceof JViewport) {
|
---|
39 | JViewport viewport = (JViewport) parent;
|
---|
40 | Rectangle rect = getCellRect(row, col, true);
|
---|
41 | Point pt = viewport.getViewPosition();
|
---|
42 | rect.setLocation(rect.x - pt.x, rect.y - pt.y);
|
---|
43 | viewport.scrollRectToVisible(rect);
|
---|
44 | }
|
---|
45 | }
|
---|
46 | }
|
---|