Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JTable Scrolling to a Specified Row Index

I have a JTable that is within a JScrollPane. Rows are added to the table at runtime based on events that happen in my application. I want to have the scoll pane scroll to the bottom of the table when a new row is added to the table.

For JLists There is the [ensureIndexIsVisible][1]() that forces a particular index in the list to be visible. I'm looking for the same thing but for a JTable. It looks like I might have to manually move the scrolling view on the scroll pane but I figured there had to be an easier way.

like image 400
Chris Dail Avatar asked May 12 '09 14:05

Chris Dail


People also ask

How can I tell if a row is selected in JTable?

JTable has a getSelectionModel() method which will give you a ListSelectionModel object. It tells you what rows are selected. You can add a ListSelectionListener to that via the addL..S..

What is JTable TableModel?

The TableModel interface specifies the methods the JTable will use to interrogate a tabular data model. The JTable can be set up to display any data model which implements the TableModel interface with a couple of lines of code: TableModel myData = new MyTableModel(); JTable table = new JTable(myData);

How does JTable work?

The JTable class is a part of Java Swing Package and is generally used to display or edit two-dimensional data that is having both rows and columns. It is similar to a spreadsheet. This arranges data in a tabular form.

How can count number of rows in JTable?

You can use the getRowCount() method: Returns the number of rows that can be shown in the JTable , given unlimited space. If a RowSorter with a filter has been specified, the number of rows returned may differ from that of the underlying TableModel .


4 Answers

It's very easy, JTable has scrollRectToVisible method too. If you want, you can try something like this to make scrollpane go to to the bottom if a new record is added :

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

Where i is last added record.

like image 105
martinusadyh Avatar answered Nov 03 '22 11:11

martinusadyh


See this example : http://www.exampledepot.com/egs/javax.swing.table/Vis.html

update: the link is now obsolete, here is the code (from 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);
    }
like image 37
Pierre Avatar answered Nov 03 '22 12:11

Pierre


JList internally use scrollRectToVisible and specify the coordinates to scroll to. I think you will have to recode a similar functionality for JTable.

like image 6
Valentin Rocher Avatar answered Nov 03 '22 13:11

Valentin Rocher


The first answer works well, but the selected row gets positioned at the bottom of the table. So I created this modified version:

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

Now the selected row gets positioned at the top of the table.

like image 1
MGaidamak Avatar answered Nov 03 '22 11:11

MGaidamak