Is there some way to know if a scrollbar is visible or not inside a JPanel? I mean, some times, my panel has many rectangles (think it as buttons) and needs a scrollbar and some times it doesn't need it. I'd like to know if I can know when it is being shown.
+1
A:
If you extend the JPanel
and add yourself the JScrollbar
s (horizontal and/or vertical), then you can control when they must be visible or invisible
(you can check if they are currently visible with the isvisible()
function)
You can find two example of such classes that determine the need for visible scrollbar depending on their content:
JGraphPanel
(its callbackactionPerformed(Event e)
will adjust the visibility based on a zoom factor)Plane
(its functionadjustComponents()
will callsetVisible()
on theJScrollBar
if needed)
VonC
2009-08-10 17:29:08
A:
Assuming you have a reference to a JScrollPane, you should be able to just call
yourJScrollPane.getHorizontalScrollBar().isVisible() and yourJScrollPane.getVerticalScrollBar().isVisible()
Joshua McKinnon
2009-08-10 18:15:09