Вопрос

У меня есть JTable, который находится внутри JScrollPane. Строки добавляются в таблицу во время выполнения на основе событий, происходящих в моем приложении.Я хочу, чтобы панель прокрутки прокручивалась до нижней части таблицы, когда в таблицу добавляется новая строка.

Для JLists существует [ensureIndexIsVisible][1]() это заставляет определенный индекс в списке быть видимым.Я ищу то же самое, но для JTable.Похоже, мне придется вручную переместить вид прокрутки на панели прокрутки, но я решил, что должен быть более простой способ.

Это было полезно?

Решение

Посмотрите этот пример: http://www.exampledepot.com/egs/javax.swing.table/Vis.html

обновлять:ссылка устарела, вот код (из 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);
    }

Другие советы

Это очень просто, в JTable тоже есть метод ScrollRectToVisible.Если хотите, вы можете попробовать что-то вроде этого, чтобы полоса прокрутки опустилась вниз, если добавлена ​​новая запись:

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

Где я это последняя добавленная запись.

JList для внутреннего использования прокруткаRectToVisible и укажите координаты для прокрутки.Я думаю, вам придется перекодировать аналогичный функционал для JTable.

Первый ответ работает хорошо, но выбранная строка оказывается внизу таблицы.Поэтому я создал эту модифицированную версию:

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);
    }

Теперь выбранная строка помещается вверху таблицы.

Мне кажется, гораздо проще установить положение области просмотра вместо прокрутки таблицы.Ниже приведен мой код.

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));
}
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top