Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.java.programmer > #24277 > unrolled thread
| Started by | Haircuts Are Important <clusardi2k@aol.com> |
|---|---|
| First post | 2013-06-03 11:37 -0700 |
| Last post | 2013-06-04 07:20 -0700 |
| Articles | 4 — 3 participants |
Back to article view | Back to comp.lang.java.programmer
How To Determine Position of JScrollPane in One JTable and Scroll Second JTable Haircuts Are Important <clusardi2k@aol.com> - 2013-06-03 11:37 -0700
Re: How To Determine Position of JScrollPane in One JTable and Scroll Second JTable Jim Gibson <jimsgibson@gmail.com> - 2013-06-03 16:56 -0700
Re: How To Determine Position of JScrollPane in One JTable and Scroll Second JTable Lew <lewbloch@gmail.com> - 2013-06-03 23:46 -0700
Re: How To Determine Position of JScrollPane in One JTable and Scroll Second JTable Haircuts Are Important <clusardi2k@aol.com> - 2013-06-04 07:20 -0700
| From | Haircuts Are Important <clusardi2k@aol.com> |
|---|---|
| Date | 2013-06-03 11:37 -0700 |
| Subject | How To Determine Position of JScrollPane in One JTable and Scroll Second JTable |
| Message-ID | <df6f8608-e823-4eb3-ac88-7a43f85b3b22@g9g2000yqh.googlegroups.com> |
How can I determine how much a JTable's JScrollPane has been moved, and then scroll a second JTable based on that movement. Thanks,
[toc] | [next] | [standalone]
| From | Jim Gibson <jimsgibson@gmail.com> |
|---|---|
| Date | 2013-06-03 16:56 -0700 |
| Message-ID | <030620131656529935%jimsgibson@gmail.com> |
| In reply to | #24277 |
In article <df6f8608-e823-4eb3-ac88-7a43f85b3b22@g9g2000yqh.googlegroups.com>, Haircuts Are Important <clusardi2k@aol.com> 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
[toc] | [prev] | [next] | [standalone]
| From | Lew <lewbloch@gmail.com> |
|---|---|
| Date | 2013-06-03 23:46 -0700 |
| Message-ID | <7fc572b9-b217-4b45-a78f-c953f81fcb33@googlegroups.com> |
| In reply to | #24279 |
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/docs/api/javax/swing/JComboBox.html#addActionListener(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
[toc] | [prev] | [next] | [standalone]
| From | Haircuts Are Important <clusardi2k@aol.com> |
|---|---|
| Date | 2013-06-04 07:20 -0700 |
| Message-ID | <b8533a95-8ce7-44ab-ba23-9d21fc5aaba8@j8g2000yqa.googlegroups.com> |
| In reply to | #24279 |
On Jun 3, 7:56 pm, Jim Gibson <jimsgib...@gmail.com> wrote:
> In article
> <df6f8608-e823-4eb3-ac88-7a43f85b3...@g9g2000yqh.googlegroups.com>,
> Haircuts Are Important <clusard...@aol.com> 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);
}
}
});
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.java.programmer
csiph-web