How To Determine Position of JScrollPane in One JTable and ScrollSecond JTable

Discussion in 'Java' started by Haircuts Are Important, Jun 3, 2013.

  1. How can I determine how much a JTable's JScrollPane has been moved,
    and then scroll a second JTable based on that movement.

    Thanks,
     
    Haircuts Are Important, Jun 3, 2013
    #1
    1. Advertising

  2. Haircuts Are Important

    Jim Gibson Guest

    Re: How To Determine Position of JScrollPane in One JTable and Scroll Second JTable

    In article
    <>,
    Haircuts Are Important <> wrote:

    > How can I determine how much a JTable's JScrollPane has been moved,
    > and then scroll a second JTable based on that movement.


    JScrollPane has two JScrollBar members: horizontalScrollBar and
    verticalScrollBar. Use the getVerticalScrollBar() method of
    JScrollPane, for example, to get the vertical scroll bar object.

    Then use the getValue() method of JScrollBar to find out where the
    scrollbar is positioned. You can define an AdjustmentListener object to
    listen for scroll bar events. Then use the setValue() method to apply
    the same offset to the other JScrollPane (assuming everything else
    about the two JScrollPanes is equal).

    --
    Jim Gibson
     
    Jim Gibson, Jun 4, 2013
    #2
    1. Advertising

  3. Haircuts Are Important

    Lew Guest

    Jim Gibson wrote:
    > Haircuts Are Important wrote:
    >> How can I determine how much a JTable's JScrollPane has been moved,
    >> and then scroll a second JTable based on that movement.

    >
    > JScrollPane has two JScrollBar members: horizontalScrollBar and
    > verticalScrollBar. Use the getVerticalScrollBar() method of
    > JScrollPane, for example, to get the vertical scroll bar object.
    >
    > Then use the getValue() method of JScrollBar to find out where the
    > scrollbar is positioned. You can define an AdjustmentListener object to
    > listen for scroll bar events. Then use the setValue() method to apply
    > the same offset to the other JScrollPane (assuming everything else
    > about the two JScrollPanes is equal).


    That is a beautifully succinct and informative post that I shall find
    useful. Thank you.

    OP (Original Poster): You can generalize from Jim's answer to coordinate
    arbitrary widgets.

    You can apply Jim's suggestion by using the Javadocs to find an equivalent
    listener for the widget you want to track.

    For example, some screen designs involve coordinated drop-down selections
    and text or choices in other screen elements. For the drop-down widget you
    use a JComboBox. Instead of an AdjustmentListener, you'd define an ActionListener and add it to the JComboBox with

    http://docs.oracle.com/javase/7/doc...dActionListener(java.awt.event.ActionListener)

    You might update the contents of a prompt elsewhere on the screen with the
    choice made in the combo box, using the listener to trigger the update.

    Extending into another dimension, if you study the "model-view-controller"
    (MVC) architecture of a good Swing screen, the same approach allows multiple
    widgets to update based on a change to a data or object model. For example,
    a stock-market ticker app could use a listener to a stock-price model
    to update a visual chart of the price, at the same time as another part
    of the display logic updates a moving average line on that chart using a
    listener to get the latest data from the model.

    --
    Lew
     
    Lew, Jun 4, 2013
    #3
  4. On Jun 3, 7:56 pm, Jim Gibson <> wrote:
    > In article
    > <>,
    > Haircuts Are Important <> wrote:
    >
    > > How can I determine how much a  JTable's JScrollPane has been moved,
    > > and then scroll a second JTable based on that movement.

    >
    > JScrollPane has two JScrollBar members: horizontalScrollBar and
    > verticalScrollBar. Use the getVerticalScrollBar() method of
    > JScrollPane, for example, to get the vertical scroll bar object.
    >
    > Then use the getValue() method of JScrollBar to find out where the
    > scrollbar is positioned. You can define an AdjustmentListener object to
    > listen for scroll bar events. Then use the setValue() method to apply
    > the same offset to the other JScrollPane (assuming everything else
    > about the two JScrollPanes is equal).
    >


    I inserted the below code, but it did not work! Per
    addAdjustmentListener, the two JScrollBars were always updated to the
    same position, but the left JTable would not scroll with the right
    JTable. The two tables have the same number of rows but differ in the
    number of columns. What is wrong. I posted most of the project in a
    recent posting.

    JScrollPane leftScrollPane = new JScrollPane(fixedTable);
    final JScrollBar leftVerticalScrollBar =
    leftScrollPane.getVerticalScrollBar();

    JScrollPane jScrollPane1 = new JScrollPane(scrollTable);
    final JScrollBar rightVerticalScrollBar =
    jScrollPane1.getVerticalScrollBar();

    rightVerticalScrollBar.addAdjustmentListener(new AdjustmentListener(){
    public void adjustmentValueChanged(AdjustmentEvent evt){
    int rightPosition = rightVerticalScrollBar.getValue();
    int lefttPosition = leftVerticalScrollBar.getValue();

    if (rightPositon != leftPosition){
    leftVerticalScrollBar.setValue(rightPosition);
    }
    }
    });
     
    Haircuts Are Important, Jun 4, 2013
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Happy Day
    Replies:
    5
    Views:
    8,208
    Rogan Dawes
    Aug 17, 2005
  2. Allan Valeriano

    Set size of JTable inside a JScrollPane

    Allan Valeriano, Jun 15, 2007, in forum: Java
    Replies:
    1
    Views:
    13,206
    Andrew Thompson
    Jun 16, 2007
  3. Replies:
    3
    Views:
    1,604
    timjowers
    Jul 19, 2007
  4. yashiro

    Jscrollpane and jtable

    yashiro, May 17, 2008, in forum: Java
    Replies:
    2
    Views:
    1,947
    Jeff Higgins
    May 17, 2008
  5. Peña, Botp
    Replies:
    1
    Views:
    242
    Robert Klemme
    Jan 24, 2004
Loading...

Share This Page