Example usage for javax.swing JTable scrollRectToVisible

List of usage examples for javax.swing JTable scrollRectToVisible

Introduction

In this page you can find the example usage for javax.swing JTable scrollRectToVisible.

Prototype

public void scrollRectToVisible(Rectangle aRect) 

Source Link

Document

Forwards the scrollRectToVisible() message to the JComponent's parent.

Usage

From source file:Main.java

public static void scroll(JTable table, int row) {
    table.scrollRectToVisible(new Rectangle(table.getCellRect(row, 0, true)));
}

From source file:com.SCI.centraltoko.utility.UtilityTools.java

public static void scroolTorect(JTable tabel, int nextRow) {
    Rectangle currenVisible = tabel.getVisibleRect();
    Rectangle scroolTorect = tabel.getCellRect(nextRow, 0, true);
    if (scroolTorect.getY() > currenVisible.getY() + currenVisible.getHeight()) {
        scroolTorect.setLocation(0,/* w w w  .ja  v  a 2s. c  om*/
                (int) (scroolTorect.getY() + currenVisible.getHeight() - scroolTorect.getHeight()));
    }
    tabel.scrollRectToVisible(scroolTorect);
}

From source file:Main.java

/**
 * Scrolls a table so that a certain cell becomes visible.
 * Source: http://javaalmanac.com/egs/javax.swing.table/Vis.html
 * @param table//from  w  w w .  j  ava  2s.c o  m
 * @param rowIndex
 * @param vColIndex
 */
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);
}

From source file:de.mendelson.comm.as2.client.AS2Gui.java

/**
 * Scrolls to an entry of the passed table
 *
 * @param table Table to to scroll in//from   w w  w .j  a va  2s.  c  o m
 * @param row Row to ensure visibility
 */
private void makeRowVisible(JTable table, int row) {
    if (table.getColumnCount() == 0) {
        return;
    }
    if (row < 0 || row >= table.getRowCount()) {
        throw new IllegalArgumentException("Requested ensure visible of row " + String.valueOf(row)
                + ", table has only " + table.getRowCount() + " rows.");
    }
    Rectangle visible = table.getVisibleRect();
    Rectangle cell = table.getCellRect(row, 0, true);
    if (cell.y < visible.y) {
        visible.y = cell.y;
        table.scrollRectToVisible(visible);
    } else if (cell.y + cell.height > visible.y + visible.height) {
        visible.y = cell.y + cell.height - visible.height;
        table.scrollRectToVisible(visible);
    }
}

From source file:org.nuclos.client.ui.collect.result.ResultController.java

private ListSelectionListener newListSelectionListener(final JTable tblResult) {
    return new ListSelectionListener() {
        @Override// w  ww  . ja  v a2 s . c o  m
        public void valueChanged(ListSelectionEvent ev) {
            try {
                final ListSelectionModel lsm = (ListSelectionModel) ev.getSource();

                final CollectStateModel<?> clctstatemodel = clctctl.getCollectStateModel();
                if (clctstatemodel.getOuterState() == CollectState.OUTERSTATE_RESULT) {
                    final int iResultMode = CollectStateModel.getResultModeFromSelectionModel(lsm);
                    if (iResultMode != clctctl.getCollectStateModel().getResultMode()) {
                        clctctl.setCollectState(CollectState.OUTERSTATE_RESULT, iResultMode);
                    }
                }

                if (!ev.getValueIsAdjusting()) {
                    // Autoscroll selection. It's okay to do that here rather than in the CollectStateListener.
                    if (!lsm.isSelectionEmpty() && tblResult.getAutoscrolls()) {
                        // ensure that the last selected row is visible:
                        final int iRowIndex = lsm.getLeadSelectionIndex();
                        final int iColIndex = tblResult.getSelectedColumn();
                        final Rectangle rectCell = tblResult.getCellRect(iRowIndex,
                                iColIndex != -1 ? iColIndex : 0, true);
                        if (rectCell != null) {
                            tblResult.scrollRectToVisible(rectCell);
                        }
                    }
                }
            } catch (CommonBusinessException ex) {
                Errors.getInstance().showExceptionDialog(clctctl.getTab(), ex);
            }
        } // valueChanged
    };
}

From source file:org.piraso.ui.api.util.JTableUtils.java

public static void scrollTo(JTable[] tables, int rowNum) {
    if (ArrayUtils.isNotEmpty(tables)) {
        for (JTable table : tables) {
            if (table == null)
                continue;

            table.scrollRectToVisible(table.getCellRect(rowNum, 1, true));
        }/*from  w w  w .  j  av  a2s.co  m*/
    }
}

From source file:pl.otros.logview.api.gui.LogViewPanelWrapper.java

private void addRowAutoScroll() {
    dataTableModel.addTableModelListener(e -> {
        if (follow.isSelected() && e.getType() == TableModelEvent.INSERT) {
            final Runnable r = () -> {
                try {
                    JTable table = logViewPanel.getTable();
                    int row = table.getRowCount() - 1;
                    if (row > 0) {
                        Rectangle rect = table.getCellRect(row, 0, true);
                        table.scrollRectToVisible(rect);
                        table.clearSelection();
                        table.setRowSelectionInterval(row, row);
                    }/* w ww  . java  2  s  . c om*/
                } catch (IllegalArgumentException iae) {
                    // ignore..out of bounds
                    iae.printStackTrace();
                }
            };
            // Wait for JViewPort size update
            // TODO Find way to invoke this listener after viewport is notified about changes
            Runnable r2 = () -> {
                try {
                    Thread.sleep(300);
                } catch (InterruptedException ignore) {
                }
                SwingUtilities.invokeLater(r);
            };
            new Thread(r2).start();
        }
    });
}

From source file:pl.otros.logview.gui.actions.search.SearchAction.java

public void performSearch(String text, SearchDirection direction) {
    StatusObserver statusObserver = getOtrosApplication().getStatusObserver();
    JTabbedPane jTabbedPane = getOtrosApplication().getJTabbedPane();
    LogViewPanelWrapper lvPanel = (LogViewPanelWrapper) jTabbedPane.getSelectedComponent();
    if (lvPanel == null) {
        return;/*www . j a v  a 2 s  .co m*/
    }
    JTable table = lvPanel.getLogViewPanel().getTable();

    NextRowProvider nextRowProvider = NextRowProviderFactory.getFilteredTableRow(table, direction);
    SearchContext context = new SearchContext();
    context.setDataTableModel(lvPanel.getDataTableModel());
    SearchMatcher searchMatcher = null;
    String confKey = null;
    if (SearchMode.STRING_CONTAINS.equals(searchMode)) {
        searchMatcher = new StringContainsSearchMatcher(text);
        confKey = ConfKeys.SEARCH_LAST_STRING;
    } else if (SearchMode.REGEX.equals(searchMode)) {
        try {
            searchMatcher = new RegexMatcher(text);
            confKey = ConfKeys.SEARCH_LAST_REGEX;
        } catch (Exception e) {
            statusObserver.updateStatus("Error in regular expression: " + e.getMessage(),
                    StatusObserver.LEVEL_ERROR);
            return;
        }
    } else if (SearchMode.QUERY.equals(searchMode)) {
        QueryAcceptCondition acceptCondition;
        try {
            acceptCondition = new QueryAcceptCondition(text);
            searchMatcher = new AcceptConditionSearchMatcher(acceptCondition);
            confKey = ConfKeys.SEARCH_LAST_QUERY;
        } catch (RuleException e) {
            statusObserver.updateStatus("Wrong query rule: " + e.getMessage(), StatusObserver.LEVEL_ERROR);
            return;

        }
    }
    updateList(confKey, getOtrosApplication().getConfiguration(), text);
    DefaultComboBoxModel model = (DefaultComboBoxModel) getOtrosApplication().getSearchField().getModel();
    model.removeElement(text);
    model.insertElementAt(text, 0);
    model.setSelectedItem(text);
    int maxCount = getOtrosApplication().getConfiguration().getInt(ConfKeys.SEARCH_LAST_COUNT, 30);
    while (model.getSize() > maxCount) {
        model.removeElementAt(model.getSize() - 1);
    }

    context.setSearchMatcher(searchMatcher);
    SearchResult searchNext = searchEngine.searchNext(context, nextRowProvider);
    if (searchNext.isFound()) {
        int row = table.convertRowIndexToView(searchNext.getRow());
        Rectangle rect = table.getCellRect(row, 0, true);
        table.scrollRectToVisible(rect);
        table.clearSelection();
        table.setRowSelectionInterval(row, row);
        statusObserver.updateStatus(String.format("Found at row %d", row), StatusObserver.LEVEL_NORMAL);
        if (markFound) {
            lvPanel.getDataTableModel().markRows(markerColors, table.convertRowIndexToModel(row));
        }

        scrollToSearchResult(
                searchMatcher.getFoundTextFragments(
                        lvPanel.getDataTableModel().getLogData(table.convertRowIndexToModel(row))),
                lvPanel.getLogViewPanel().getLogDetailTextArea());
    } else {
        statusObserver.updateStatus(String.format("\"%s\" not found", text), StatusObserver.LEVEL_WARNING);
    }
}

From source file:pl.otros.logview.gui.LogViewPanelWrapper.java

private void addRowScroller() {
    dataTableModel.addTableModelListener(new TableModelListener() {

        @Override/*from   www .  j  a  v a2s.co  m*/
        public void tableChanged(final TableModelEvent e) {
            if (follow.isSelected() && e.getType() == TableModelEvent.INSERT) {
                final Runnable r = new Runnable() {

                    @Override
                    public void run() {
                        try {
                            JTable table = logViewPanel.getTable();
                            int row = table.getRowCount() - 1;
                            if (row > 0) {
                                Rectangle rect = table.getCellRect(row, 0, true);
                                table.scrollRectToVisible(rect);
                                table.clearSelection();
                                table.setRowSelectionInterval(row, row);
                            }
                        } catch (IllegalArgumentException iae) {
                            // ignore..out of bounds
                            iae.printStackTrace();
                        }
                    }
                };
                // Wait for JViewPort size update
                // TODO Find way to invoke this listener after viewport is notified about changes
                Runnable r2 = new Runnable() {

                    @Override
                    public void run() {
                        try {
                            Thread.sleep(300);
                        } catch (InterruptedException ignore) {
                        }
                        SwingUtilities.invokeLater(r);
                    }
                };
                new Thread(r2).start();
            }
        }
    });
}