Example usage for javax.swing JViewport getViewPosition

List of usage examples for javax.swing JViewport getViewPosition

Introduction

In this page you can find the example usage for javax.swing JViewport getViewPosition.

Prototype

public Point getViewPosition() 

Source Link

Document

Returns the view coordinates that appear in the upper left hand corner of the viewport, or 0,0 if there's no view.

Usage

From source file:Main.java

public static void main(String[] args) {
    String text = "one\ntwo\nthree\nfour\nfive";
    JFrame frame = new JFrame("title");
    JTextArea textArea = new JTextArea(text, 1, 30); // shows only one line
    JScrollPane scrollPane = new JScrollPane(textArea);
    frame.add(scrollPane);/*from ww w . ja  va2  s  .co  m*/
    frame.pack();
    frame.setVisible(true);

    final JViewport viewport = scrollPane.getViewport();

    textArea.addCaretListener(e -> {
        System.out.println("First : " + viewport.getViewPosition());
        System.out.println("Second: " + viewport.getViewPosition());
    });
    textArea.setCaretPosition(text.length());
}

From source file:Main.java

public static void scrollToVisible(JTable table, int rowIndex, int vColIndex) {
    if (!(table.getParent() instanceof JViewport)) {
        return;/*w  w w.  java2  s.  c  o  m*/
    }
    JViewport viewport = (JViewport) table.getParent();
    Rectangle rect = table.getCellRect(rowIndex, vColIndex, true);
    Point pt = viewport.getViewPosition();
    rect.setLocation(rect.x - pt.x, rect.y - pt.y);
    viewport.scrollRectToVisible(rect);
}

From source file:Main.java

public static boolean isCellVisible(JTable table, int rowIndex, int vColIndex) {
    if (!(table.getParent() instanceof JViewport)) {
        return false;
    }//from w  w  w .  ja  v a  2s  .c  o m
    JViewport viewport = (JViewport) table.getParent();
    Rectangle rect = table.getCellRect(rowIndex, vColIndex, true);
    Point pt = viewport.getViewPosition();
    rect.setLocation(rect.x - pt.x, rect.y - pt.y);
    return new Rectangle(viewport.getExtentSize()).contains(rect);
}

From source file:Main.java

public static void addMiddleButtonDragSupport(Component targetComponent) {
    MouseInputAdapter mia = new MouseInputAdapter() {
        int m_XDifference, m_YDifference;
        boolean m_dragging = false;

        public void mouseDragged(MouseEvent e) {
            if (!m_dragging)
                return;

            Component target = e.getComponent();
            Container c = target.getParent();
            if (c instanceof JViewport) {
                JViewport jv = (JViewport) c;
                Point p = jv.getViewPosition();
                int newX = p.x - (e.getX() - m_XDifference);
                int newY = p.y - (e.getY() - m_YDifference);

                int maxX = target.getWidth() - jv.getWidth();
                int maxY = target.getHeight() - jv.getHeight();
                if (newX < 0)
                    newX = 0;//from   w ww .  j a  v  a 2  s  .  c o m
                if (newX > maxX)
                    newX = maxX;
                if (newY < 0)
                    newY = 0;
                if (newY > maxY)
                    newY = maxY;

                jv.setViewPosition(new Point(newX, newY));
            }
        }

        Cursor oldCursor;

        public void mousePressed(MouseEvent e) {
            if (SwingUtilities.isMiddleMouseButton(e)) {
                m_dragging = true;
                oldCursor = e.getComponent().getCursor();
                e.getComponent().setCursor(Cursor.getPredefinedCursor(Cursor.MOVE_CURSOR));
                m_XDifference = e.getX();
                m_YDifference = e.getY();
            }
        }

        public void mouseReleased(MouseEvent e) {
            if (m_dragging) {
                e.getComponent().setCursor(oldCursor);
                m_dragging = false;
            }
        }
    };
    targetComponent.addMouseMotionListener(mia);
    targetComponent.addMouseListener(mia);
}

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  ww  w.  j  ava  2 s.  c  om*/
 * @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:Main.java

/** 
 * Assumes table is contained in a JScrollPane.
 * Scrolls the cell (rowIndex, vColIndex) so that it is visible
 * within the viewport./*w w w.ja v a2 s .  co m*/
 */
public static void scrollToVisible(JTable table, int row, int col) {
    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(row, col, 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);

    // Scroll the area into view
    viewport.scrollRectToVisible(rect);
}

From source file:Main.java

public static void synchronizeView(final JViewport masterViewport, final JViewport slaveViewport,
        final int orientation) {
    final ChangeListener c1 = new ChangeListener() {
        public void stateChanged(ChangeEvent e) {
            if (masterViewport.getView() == null || slaveViewport.getView() == null) {
                return;
            }//from   w w  w .  j a  va2s .co  m
            if (orientation == SwingConstants.HORIZONTAL) {
                Point v1 = masterViewport.getViewPosition();
                Point v2 = slaveViewport.getViewPosition();
                if (v1.x != v2.x) {
                    slaveViewport.setViewPosition(new Point(v1.x, v2.y));
                }
            } else if (orientation == SwingConstants.VERTICAL) {
                Point v1 = masterViewport.getViewPosition();
                Point v2 = slaveViewport.getViewPosition();
                if (v1.y != v2.y) {
                    slaveViewport.setViewPosition(new Point(v2.x, v1.y));
                }
            }
        }
    };

    masterViewport.addChangeListener(c1);
}

From source file:Main.java

public Main() {
    super();/* w  ww.j  a v  a 2s .c om*/
    JTree tree = new JTree();
    for (int i = 0; i < tree.getRowCount(); i++) {
        tree.expandRow(i);
    }

    final JScrollPane pane = new JScrollPane(tree) {
        Dimension prefSize = new Dimension(200, 150);

        public Dimension getPreferredSize() {
            return prefSize;
        }
    };

    pane.getVerticalScrollBar().addAdjustmentListener(e -> {
        JViewport vp = pane.getViewport();
        if (vp.getView().getHeight() <= vp.getHeight() + vp.getViewPosition().y) {
            System.out.println("End");
        }
    });

    add(pane);
}

From source file:Main.java

public GrabAndScrollLabel(ImageIcon i) {
    super(i);/*  ww w  .j  a  va  2  s. co  m*/

    MouseInputAdapter mia = new MouseInputAdapter() {
        int xDiff, yDiff;
        Container c;

        public void mouseDragged(MouseEvent e) {
            c = GrabAndScrollLabel.this.getParent();
            if (c instanceof JViewport) {
                JViewport jv = (JViewport) c;
                Point p = jv.getViewPosition();
                int newX = p.x - (e.getX() - xDiff);
                int newY = p.y - (e.getY() - yDiff);

                int maxX = GrabAndScrollLabel.this.getWidth() - jv.getWidth();
                int maxY = GrabAndScrollLabel.this.getHeight() - jv.getHeight();
                if (newX < 0)
                    newX = 0;
                if (newX > maxX)
                    newX = maxX;
                if (newY < 0)
                    newY = 0;
                if (newY > maxY)
                    newY = maxY;

                jv.setViewPosition(new Point(newX, newY));
            }
        }

        public void mousePressed(MouseEvent e) {
            setCursor(Cursor.getPredefinedCursor(Cursor.MOVE_CURSOR));
            xDiff = e.getX();
            yDiff = e.getY();
        }

        public void mouseReleased(MouseEvent e) {
            setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
        }
    };
    addMouseMotionListener(mia);
    addMouseListener(mia);
}

From source file:GrabAndDragDemo.java

public GrabAndScrollLabel(ImageIcon i) {
    super(i);/*from  ww  w . j a va  2 s.  co m*/

    MouseInputAdapter mia = new MouseInputAdapter() {
        int xDiff, yDiff;

        boolean isDragging;

        Container c;

        public void mouseDragged(MouseEvent e) {
            c = GrabAndScrollLabel.this.getParent();
            if (c instanceof JViewport) {
                JViewport jv = (JViewport) c;
                Point p = jv.getViewPosition();
                int newX = p.x - (e.getX() - xDiff);
                int newY = p.y - (e.getY() - yDiff);

                int maxX = GrabAndScrollLabel.this.getWidth() - jv.getWidth();
                int maxY = GrabAndScrollLabel.this.getHeight() - jv.getHeight();
                if (newX < 0)
                    newX = 0;
                if (newX > maxX)
                    newX = maxX;
                if (newY < 0)
                    newY = 0;
                if (newY > maxY)
                    newY = maxY;

                jv.setViewPosition(new Point(newX, newY));
            }
        }

        public void mousePressed(MouseEvent e) {
            setCursor(Cursor.getPredefinedCursor(Cursor.MOVE_CURSOR));
            xDiff = e.getX();
            yDiff = e.getY();
        }

        public void mouseReleased(MouseEvent e) {
            setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
        }
    };
    addMouseMotionListener(mia);
    addMouseListener(mia);
}