Skip to main content

linked Scroll panes ...

4 replies [Last post]
cmllpz
Offline
Joined: 2008-08-09
Points: 0

~
 I need to link the scrolling behaviour of various text components so that a repositioning/update in one will cause the other one to repositioning/update as well
~
 Look at this Swing example:

~

 download.oracle.com/javase/tutorialJWS/uiswing/components/ex6/TextComponentDemo.jnlp 
~
 If you type enough lines so that the scroll bar quicks in and then move your cursor up and down the pane below the pane on the scroll of the top does not accordingly reset its pane
~
 How can you achieve something like that? Any examples out there?
~
 Thanks
 lbrtchx

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
walterln
Offline
Joined: 2007-04-17
Points: 0

The joys of Model View Controller
Well, looks like posting code is useless here. Try the oracle http://forums.oracle.com/forums/forum.jspa?forumID=950 forum instead.

import java.awt.EventQueue;<br />
import javax.swing.*;</p>
<p>public class TestSyncScroll {<br />
    public static void main(String[] args) {<br />
        EventQueue.invokeLater(new Runnable() {<br />
            public void run() {<br />
                JTextArea text1 = new JTextArea(5, 10);<br />
                JTextArea text2 = new JTextArea(5, 10);<br />
                text2.setDocument(text1.getDocument());</p>
<p>                JScrollPane scroll1 = new JScrollPane(text1);<br />
                JScrollPane scroll2 = new JScrollPane(text2);<br />
                scroll2.getHorizontalScrollBar().setModel(<br />
                        scroll1.getHorizontalScrollBar().getModel());<br />
                scroll2.getVerticalScrollBar().setModel(scroll1.getVerticalScrollBar().getModel());</p>
<p>                Box box = new Box(BoxLayout.PAGE_AXIS);<br />
                box.add(scroll1);<br />
                box.add(scroll2);</p>
<p>                JFrame frame = new JFrame("Test");<br />
                frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);<br />
                frame.getContentPane().add(box);<br />
                frame.pack();<br />
                frame.setLocationRelativeTo(null);<br />
                frame.setVisible(true);<br />
            }<br />
        });<br />
    }<br />
}<br />

cmllpz
Offline
Joined: 2008-08-09
Points: 0

~
Well, I thought your code example had made my day, but then when I used a JTextPane and set it as not editable even though I could not edit it directly I could see how my editions on the JTextArea were reflected on the JTextPane. Of course, it was just wishful thinking! ;-)
~
So, I only need to sync the scrolling part of model, not all of it. I will have to tinker deeply with the models and/or maybe using the model Listerner?
~
Any examples of such thing out there, even on a book?
~
Thanks
lbrtchx

walterln
Offline
Joined: 2007-04-17
Points: 0

You could try to understand the code of course :). If you don't want the text to be the same, don't set the Document to the other text component. Only use the same scrollbar models for both.

cmllpz
Offline
Joined: 2008-08-09
Points: 0

~
I mentioned Scroll panes because they are functionally linked to some extent so I thought getting this functionality from them would be easier, but of course they could be any two text components
~
lbrtchx