The Artima Developer Community
Sponsored Link

Java Answers Forum
How to make JPanel scrollable?

4 replies on 1 page. Most recent reply: Feb 3, 2004 1:20 PM by Joseph Lee

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 4 replies on 1 page
Joseph Lee

Posts: 10
Nickname: at9483mph
Registered: Jan, 2004

How to make JPanel scrollable? Posted: Jan 8, 2004 12:50 PM
Reply to this message Reply
Advertisement
I have an object(Drawing) extends the JPanel, I try to make it scrollable but when any component painted outside the jpanel, it doesn't show scrollbar. Can anyone please advise me?

Drawing draw= new Drawing();

JScrollPane scroller = new JScrollPane(draw);
JPanel jp = new JPanel(new FlowLayout(FlowLayout.LEFT));
jp.add(scroller);
getContentPane.add(jp);
show();


mausam

Posts: 243
Nickname: mausam
Registered: Sep, 2003

Re: How to make JPanel scrollable? Posted: Jan 8, 2004 9:49 PM
Reply to this message Reply
What i did once i was facing this problem is that

i made my Class to implemen Runnable interface

and in run method did smething like this

public void run()
{
  while(true)
  {
      SwingUtilities.invokeLater(new Runnable()
      {
            public void run()
            {
             scroller .getVerticalScrollBar().setValue(scroller .getVerticalScrollBar().getMaximum());  
            }
        });
 
          try
          {
                   Thread.currentThread().sleep(1000);
         }
           catch(Exception e){}
       }
       
   }
 


HTH...

thanks
Aminur

Audra

Posts: 1
Nickname: domino
Registered: Jan, 2004

Re: How to make JPanel scrollable? Posted: Jan 29, 2004 2:48 PM
Reply to this message Reply
Hrm, seems pretty convoluted. My uneducated first guess would be that adding the JScrollPane to a JPanel and then adding the JPanel could interfere with JScrollPane's ability to scroll, but that could be entirely false. In any case, draw is already extended from JPanel and then added to scroller. jp seems like one extra component too many. Adding scroller directly to the content pane should do this here; the content pane shouldn't be limited to taking only JPanels, it can take JScrollPanes directly, unless there's a specific reason you're adding the extra JPanel.

Drawing draw= new Drawing();

JScrollPane scroller = new JScrollPane(draw);
getContentPane.add(scroller);
show();

mausam

Posts: 243
Nickname: mausam
Registered: Sep, 2003

Re: How to make JPanel scrollable? Posted: Jan 29, 2004 8:46 PM
Reply to this message Reply
Gosh..

I thought u have added ur panel to the scroll bar and it is not scrolling to the desired location at run time when u add a picture or something to panel.

Thats why that code was there.

Otherwise ur code isfine to work for

Joseph Lee

Posts: 10
Nickname: at9483mph
Registered: Jan, 2004

Re: How to make JPanel scrollable? Posted: Feb 3, 2004 1:20 PM
Reply to this message Reply
Dear Audra and mausam,

Thanks for you advise!! I have did it in another way which is I over write the getPreferredSize() method in the Drawing object where I get the boundaries of the objects I added...but I not sure is that the proper way to do but it's works!!! THANKS!!

My code for that method are:

public Dimension getPreferredSize()
{
Dimension tmp = new Dimension();
Rectangle2D bound = dp.getBoundary((Graphics2D)getGraphics());
if(bound!=null)
tmp = new Dimension((int)( bound.getMaxX()*zoom), (int)(bound.getMaxY()));
else
tmp = getSize(tmp);
return tmp;
}

Flat View: This topic has 4 replies on 1 page
Topic: applet number generator Previous Topic   Next Topic Topic: java.lang.NullPointerException at MyField.actionPerformed(MyField.java:580)

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use