Results 1 to 12 of 12

Thread: JPanel

  1. #1
    Join Date
    Apr 2010
    Posts
    1
    Rep Power
    0

    Angry JPanel

    I have two JPanel but i am unable to scrolling it simultenously.
    Please help.
    Thanks in Advance.

  2. #2
    j2me64's Avatar
    j2me64 is offline Senior Member
    Join Date
    Sep 2009
    Location
    Zurich, Switzerland
    Posts
    962
    Rep Power
    6

    Default

    Quote Originally Posted by Jitendra Shukla View Post
    I have two JPanel but i am unable to scrolling it simultenously.
    Please help.
    Thanks in Advance.

    do you mean you need a vertical scrollbar? post you code.

  3. #3
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by Jitendra Shukla View Post
    I have two JPanel but i am unable to scrolling it simultenously.
    Please help.
    Thanks in Advance.
    If you have two scrollpanes that both hold JPanels that are the same size, you can get them to scroll in synchrony by having the vertical (or horizontal if that's the direction you're gearing at) JScrollBars share the same model, but mainly I second the recommendation that you post code as it's impossible to guess what your problem is based on the information you've provided so far.

  4. #4
    Webuser is offline Senior Member
    Join Date
    Dec 2008
    Posts
    526
    Rep Power
    0

    Lightbulb

    Quote Originally Posted by Jitendra Shukla View Post
    I have two JPanel but i am unable to scrolling it simultenously.
    Please help.
    Thanks in Advance.
    It is easy

    Java Code:
    JPanel a=new JPanel();
    JScrollPane scroll=new JScrollPane();
    scroll.getViewport().add(a,null);
    If my answer helped you. Please click my "REP" button and add a comment
    Have a Good Java Coding :)

  5. #5
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,508
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by Webuser View Post
    It is easy

    Java Code:
    JPanel a=new JPanel();
    JScrollPane scroll=new JScrollPane();
    scroll.getViewport().add(a,null);
    Yes, that scrolls one single JPanel; the OP wants to scroll two of them simultaneously; didn't you read the question?

    kind regards,

    Jos

  6. #6
    Webuser is offline Senior Member
    Join Date
    Dec 2008
    Posts
    526
    Rep Power
    0

    Lightbulb

    Quote Originally Posted by JosAH View Post
    Yes, that scrolls one single JPanel; the OP wants to scroll two of them simultaneously; didn't you read the question?

    kind regards,

    Jos
    It is easy to guess that you can simply connect the previous code to some simple modifications :)

    Java Code:
    JScrollPane s1=new JScrollPane();
    JScrollPane s2=new JScrollPane();
    JScrollBar horizons1= s1.getHorizontalScrollBar();     
    JScrollBar horizons2=s2.getHorizontalScrollBar();     
    JScrollBar verticals1=s1.getVerticalScrollBar();    
    JScrollBar verticals2=s2.getVerticalScrollBar();
    
    horizons2.setValue(horizons1.getValue());
    verticals2.setValue(verticals1.getValue());
    If my answer helped you. Please click my "REP" button and add a comment
    Have a Good Java Coding :)

  7. #7
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by Webuser View Post
    It is easy to guess that you can simply connect the previous code to some simple modifications :)

    Java Code:
    JScrollPane s1=new JScrollPane();
    JScrollPane s2=new JScrollPane();
    JScrollBar horizons1= s1.getHorizontalScrollBar();     
    JScrollBar horizons2=s2.getHorizontalScrollBar();     
    JScrollBar verticals1=s1.getVerticalScrollBar();    
    JScrollBar verticals2=s2.getVerticalScrollBar();
    
    horizons2.setValue(horizons1.getValue());
    verticals2.setValue(verticals1.getValue());
    And how is better than my suggestion about sharing JScrollBar models? How does this update the scrollbar positions while the program is running?
    Last edited by Fubarable; 04-24-2010 at 05:01 AM.

  8. #8
    Webuser is offline Senior Member
    Join Date
    Dec 2008
    Posts
    526
    Rep Power
    0

    Lightbulb

    Of course the code

    Java Code:
    horizons2.setValue(horizons1.getValue());
    verticals2.setValue(verticals1.getValue());
    is for JScrollBar action listener method



    Java Code:
    AdjustmentListener adjustmentListener = new AdjustmentListener() {
          public void adjustmentValueChanged(AdjustmentEvent adjustmentEvent) {
            horizons2.setValue(horizons1.getValue());
            verticals2.setValue(verticals1.getValue());
    
          }
        };
    
    horizon1.addAdjustmentListener(adjustmentListener );
    vertical1.addAdjustmentListener(adjustmentListener );
    it is also possible to use JScrollBar model

    code like a

    Java Code:
    s1.getVerticalScrollBar().setModel(s2.getVerticalScrollBar().getModel());

    I recommend read some additional info
    Scroll Control
    and read some lessons lessons
    If my answer helped you. Please click my "REP" button and add a comment
    Have a Good Java Coding :)

  9. #9
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by Webuser View Post
    Of course the code

    Java Code:
    horizons2.setValue(horizons1.getValue());
    verticals2.setValue(verticals1.getValue());
    is for JScrollBar action listener method
    but of course. So say you have four JTextAreas with identical text in four JScrollPanes. How would you use your "action listener" method to synchronize all four scrollpanes?

  10. #10
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,508
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by Fubarable View Post
    but of course. So say you have four JTextAreas with identical text in four JScrollPanes. How would you use your "action listener" method to synchronize all four scrollpanes?
    That ActionListener needs to be a mediator; it'd be a bit of convoluted code; but I agree that your suggestion of sharing a single model between the scroll bars is more elegant; WebUser's tip wasn't very thought out and no rep points although he keeps on begging for them ;-)

    kind regards,

    Jos

  11. #11
    Webuser is offline Senior Member
    Join Date
    Dec 2008
    Posts
    526
    Rep Power
    0

    Talking

    Quote Originally Posted by JosAH View Post
    That ActionListener needs to be a mediator; it'd be a bit of convoluted code; but I agree that your suggestion of sharing a single model between the scroll bars is more elegant; WebUser's tip wasn't very thought out and no rep points although he keeps on begging for them ;-)

    kind regards,

    Jos
    JosAH, I would recommend you say something useful as for Jitendra Shukla instead of criticize my real attempts to help this guy ;)
    Last edited by Webuser; 04-27-2010 at 05:37 AM.

  12. #12
    Webuser is offline Senior Member
    Join Date
    Dec 2008
    Posts
    526
    Rep Power
    0

    Lightbulb

    Quote Originally Posted by Fubarable View Post
    but of course. So say you have four JTextAreas with identical text in four JScrollPanes. How would you use your "action listener" method to synchronize all four scrollpanes?
    Oh my... Author doesn't mean to use 4 JScrollPane - s, as I can remember ;)

    Any JScrollPane contains 2 JScrollBars it is the fact )

    you just add act. listeners to JScrollBar hor1 and JScrollBar ver1 (JScrollPane p1)

    and then simply sinchronize their scrollers coordinates with JScrollBar hor2 and JScrollBar ver2 (JScrollPane p2). It is much easy :) The version I provide is supposed to use implementation. But the idea is identical )

    Java Code:
    JScrollPane s1=new JScrollPane();
    JScrollPane s2=new JScrollPane();
    JScrollBar horizons1;
    JScrollBar horizons2;
    JScrollBar verticals1;
    JScrollBar verticals2;
    
    void initComp(){
    
    horizons1= s1.getHorizontalScrollBar();     
    horizons2=s2.getHorizontalScrollBar();     
    verticals1=s1.getVerticalScrollBar();    
    verticals2=s2.getVerticalScrollBar();
    
           
    
    horizon1.addAdjustmentListener(this);
    vertical1.addAdjustmentListener(this);
    }
    
      public void adjustmentValueChanged(AdjustmentEvent adjustmentEvent) {
            horizons2.setValue(horizons1.getValue());
            verticals2.setValue(verticals1.getValue());
    
          }
    Last edited by Webuser; 04-27-2010 at 05:38 AM.
    If my answer helped you. Please click my "REP" button and add a comment
    Have a Good Java Coding :)

Similar Threads

  1. .add to a JPanel
    By harrier in forum NetBeans
    Replies: 11
    Last Post: 07-13-2010, 10:24 AM
  2. image in jPanel
    By idi in forum AWT / Swing
    Replies: 1
    Last Post: 03-13-2010, 09:30 PM
  3. I need some help with JPanel
    By bantes in forum AWT / Swing
    Replies: 7
    Last Post: 11-03-2009, 07:58 PM
  4. drawing to a JPanel
    By diggitydoggz in forum New To Java
    Replies: 11
    Last Post: 03-09-2009, 07:42 AM
  5. How to use Jpanel
    By Manfizy in forum NetBeans
    Replies: 0
    Last Post: 02-19-2009, 12:34 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •