File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Swing / AWT / SWT and the fly likes Swing and JScrollBar Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Swing and JScrollBar " Watch "Swing and JScrollBar " New topic

Swing and JScrollBar

guptabandhu roy

Joined: Mar 22, 2008
Posts: 9
HI Guys ,
Merry Christmas and Happy New Year to all of you .
Just had a few questions regarding getting the scrollbars in JScrollPane
1> What is the difference between setSize() and setPreferredSize()
2> If I need to get the scrollbars in JScrollPane do I need to set the size or preferred size of the component it contains to bigger than the JScrollPAne or the parent component of the JScrollPane ? Let me explain with an example :-

JFrame contains JScrollPane contains JDesktopPane . Now lets say I want to see the scrollbars on JScrollPane . What do I need to do ?
How big should I make the JDesktopPane ?
3> Can I detect any sort of action using a ActionListener . If so what is the need for other classes like MouseLIstener ?

Thanks in advance
Campbell Ritchie

Joined: Oct 13, 2005
Posts: 46397
What did you find out when you read the API documentation and the Java™ Tutorial?
Did you read the lists of methods in MouseListener, ActionListener, etc? Or which listeners are available in the Java™ Tutorials?

Remember an action means a button being pressed or similar; this is a "high-level" event, which you are doubtless familiar with from using web browsers, word processors, etc. Moving a mouse is a "low-level" event which you may wish to ignore. As a simple rule of thumb, it is usually better to use the highest-level event available to fulfil your requirements.
Rob Camick
Ranch Hand

Joined: Jun 13, 2009
Posts: 2454
Scrollbars appear automatically when the "preferred size" of the component added to the viewport of the scrollpane is greater than the size of the scrollpane. So somewhere you need to determine the preferred size of your desktop pane and then use:

I agree. Here's the link:
subject: Swing and JScrollBar
jQuery in Action, 3rd edition