51

行内にあるJTableがJScrollPane.、アプリケーションで発生するイベントに基づいて実行時にテーブルに追加されます。新しい行がテーブルに追加されたときに、scollペインをテーブルの一番下までスクロールさせたいです。

JLists[ensureIndexIsVisible][1]()の場合リスト内の特定のインデックスを強制的に表示するものがあります。私は同じものを探していますが、JTableを探しています。スクロールペインのスクロールビューを手動で移動する必要があるようですが、もっと簡単な方法が必要だと思いました。

4

5 に答える 5

76

非常に簡単です。JTableにはscrollRectToVisibleメソッドもあります。必要に応じて、次のような方法を試して、新しいレコードが追加された場合にスクロールペインを一番下に移動させることができます。

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

私が最後に追加されたレコードです。

于 2011-06-03T15:20:27.017 に答える
35

この例を参照してください: 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 から)。ジャバ

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);
    }
于 2009-05-12T14:24:31.367 に答える
6

JList は内部的にscrollRectToVisibleを使用し、スクロール先の座標を指定します。JTable の同様の機能を再コーディングする必要があると思います。

于 2009-05-12T14:27:02.300 に答える
1

最初の答えはうまく機能しますが、選択した行はテーブルの一番下に配置されます。だから私はこの修正バージョンを作成しました:

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

これで、選択した行がテーブルの一番上に配置されます。

于 2013-03-12T11:51:50.407 に答える
1

テーブルをスクロールする代わりに、ビューポートの位置を設定する方がずっと簡単に思えます。以下は私のコードです。

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));
}
于 2013-11-20T22:17:25.237 に答える