| Method from java.awt.ScrollPane Detail: |
protected final void addImpl(Component comp,
Object constraints,
int index) {
synchronized (getTreeLock()) {
if (getComponentCount() > 0) {
remove(0);
}
if (index > 0) {
throw new IllegalArgumentException("position greater than 0");
}
if (!SunToolkit.isLightweightOrUnknown(comp)) {
super.addImpl(comp, constraints, index);
} else {
addToPanel(comp, constraints, index);
}
}
}
Adds the specified component to this scroll pane container.
If the scroll pane has an existing child component, that
component is removed and the new one is added. |
public void addNotify() {
synchronized (getTreeLock()) {
int vAdjustableValue = 0;
int hAdjustableValue = 0;
// Bug 4124460. Save the current adjustable values,
// so they can be restored after addnotify. Set the
// adjustables to 0, to prevent crashes for possible
// negative values.
if (getComponentCount() > 0) {
vAdjustableValue = vAdjustable.getValue();
hAdjustableValue = hAdjustable.getValue();
vAdjustable.setValue(0);
hAdjustable.setValue(0);
}
if (peer == null)
peer = getToolkit().createScrollPane(this);
super.addNotify();
// Bug 4124460. Restore the adjustable values.
if (getComponentCount() > 0) {
vAdjustable.setValue(vAdjustableValue);
hAdjustable.setValue(hAdjustableValue);
}
}
}
Creates the scroll pane's peer. |
void autoProcessMouseWheel(MouseWheelEvent e) {
processMouseWheelEvent(e);
}
|
Dimension calculateChildSize() {
//
// calculate the view size, accounting for border but not scrollbars
// - don't use right/bottom insets since they vary depending
// on whether or not scrollbars were displayed on last resize
//
Dimension size = getSize();
Insets insets = getInsets();
int viewWidth = size.width - insets.left*2;
int viewHeight = size.height - insets.top*2;
//
// determine whether or not horz or vert scrollbars will be displayed
//
boolean vbarOn;
boolean hbarOn;
Component child = getComponent(0);
Dimension childSize = new Dimension(child.getPreferredSize());
if (scrollbarDisplayPolicy == SCROLLBARS_AS_NEEDED) {
vbarOn = childSize.height > viewHeight;
hbarOn = childSize.width > viewWidth;
} else if (scrollbarDisplayPolicy == SCROLLBARS_ALWAYS) {
vbarOn = hbarOn = true;
} else { // SCROLLBARS_NEVER
vbarOn = hbarOn = false;
}
//
// adjust predicted view size to account for scrollbars
//
int vbarWidth = getVScrollbarWidth();
int hbarHeight = getHScrollbarHeight();
if (vbarOn) {
viewWidth -= vbarWidth;
}
if(hbarOn) {
viewHeight -= hbarHeight;
}
//
// if child is smaller than view, size it up
//
if (childSize.width < viewWidth) {
childSize.width = viewWidth;
}
if (childSize.height < viewHeight) {
childSize.height = viewHeight;
}
return childSize;
}
Determine the size to allocate the child component.
If the viewport area is bigger than the childs
preferred size then the child is allocated enough
to fill the viewport, otherwise the child is given
it's preferred size. |
String constructComponentName() {
synchronized (ScrollPane.class) {
return base + nameCounter++;
}
}
Construct a name for this component. Called by getName() when the
name is null. |
public void doLayout() {
layout();
}
Lays out this container by resizing its child to its preferred size.
If the new preferred size of the child causes the current scroll
position to be invalid, the scroll position is set to the closest
valid position. |
protected boolean eventTypeEnabled(int type) {
if (type == MouseEvent.MOUSE_WHEEL && isWheelScrollingEnabled()) {
return true;
}
else {
return super.eventTypeEnabled(type);
}
}
If wheel scrolling is enabled, we return true for MouseWheelEvents |
public AccessibleContext getAccessibleContext() {
if (accessibleContext == null) {
accessibleContext = new AccessibleAWTScrollPane();
}
return accessibleContext;
}
Gets the AccessibleContext associated with this ScrollPane.
For scroll panes, the AccessibleContext takes the form of an
AccessibleAWTScrollPane.
A new AccessibleAWTScrollPane instance is created if necessary. |
public Adjustable getHAdjustable() {
return hAdjustable;
}
Returns the ScrollPaneAdjustable object which
represents the state of the horizontal scrollbar.
The declared return type of this method is
Adjustable to maintain backward compatibility. |
public int getHScrollbarHeight() {
int h = 0;
if (scrollbarDisplayPolicy != SCROLLBARS_NEVER) {
ScrollPanePeer peer = (ScrollPanePeer)this.peer;
if (peer != null) {
h = peer.getHScrollbarHeight();
}
}
return h;
}
Returns the height that would be occupied by a horizontal
scrollbar, which is independent of whether it is currently
displayed by the scroll pane or not. |
public Point getScrollPosition() {
if (ncomponents < = 0) {
throw new NullPointerException("child is null");
}
return new Point(hAdjustable.getValue(), vAdjustable.getValue());
}
Returns the current x,y position within the child which is displayed
at the 0,0 location of the scrolled panel's view port.
This is a convenience method which interfaces with the adjustable
objects which represent the state of the scrollbars. |
public int getScrollbarDisplayPolicy() {
return scrollbarDisplayPolicy;
}
Returns the display policy for the scrollbars. |
public Adjustable getVAdjustable() {
return vAdjustable;
}
Returns the ScrollPaneAdjustable object which
represents the state of the vertical scrollbar.
The declared return type of this method is
Adjustable to maintain backward compatibility. |
public int getVScrollbarWidth() {
int w = 0;
if (scrollbarDisplayPolicy != SCROLLBARS_NEVER) {
ScrollPanePeer peer = (ScrollPanePeer)this.peer;
if (peer != null) {
w = peer.getVScrollbarWidth();
}
}
return w;
}
Returns the width that would be occupied by a vertical
scrollbar, which is independent of whether it is currently
displayed by the scroll pane or not. |
public Dimension getViewportSize() {
Insets i = getInsets();
return new Dimension(width - i.right - i.left,
height - i.top - i.bottom);
}
Returns the current size of the scroll pane's view port. |
public boolean isWheelScrollingEnabled() {
return wheelScrollingEnabled;
}
Indicates whether or not scrolling will take place in response to
the mouse wheel. Wheel scrolling is enabled by default. |
public void layout() {
if (ncomponents > 0) {
Component c = getComponent(0);
Point p = getScrollPosition();
Dimension cs = calculateChildSize();
Dimension vs = getViewportSize();
Insets i = getInsets();
c.reshape(i.left - p.x, i.top - p.y, cs.width, cs.height);
ScrollPanePeer peer = (ScrollPanePeer)this.peer;
if (peer != null) {
peer.childResized(cs.width, cs.height);
}
// update adjustables... the viewport size may have changed
// with the scrollbars coming or going so the viewport size
// is updated before the adjustables.
vs = getViewportSize();
hAdjustable.setSpan(0, cs.width, vs.width);
vAdjustable.setSpan(0, cs.height, vs.height);
}
} Deprecated! As - of JDK version 1.1,
replaced by doLayout().
|
public String paramString() {
String sdpStr;
switch (scrollbarDisplayPolicy) {
case SCROLLBARS_AS_NEEDED:
sdpStr = "as-needed";
break;
case SCROLLBARS_ALWAYS:
sdpStr = "always";
break;
case SCROLLBARS_NEVER:
sdpStr = "never";
break;
default:
sdpStr = "invalid display policy";
}
Point p = ncomponents > 0? getScrollPosition() : new Point(0,0);
Insets i = getInsets();
return super.paramString()+",ScrollPosition=("+p.x+","+p.y+")"+
",Insets=("+i.top+","+i.left+","+i.bottom+","+i.right+")"+
",ScrollbarDisplayPolicy="+sdpStr+
",wheelScrollingEnabled="+isWheelScrollingEnabled();
}
Returns a string representing the state of this
ScrollPane. This
method is intended to be used only for debugging purposes, and the
content and format of the returned string may vary between
implementations. The returned string may be empty but may not be
null. |
public void printComponents(Graphics g) {
if (ncomponents > 0) {
Component c = component[0];
Point p = c.getLocation();
Dimension vs = getViewportSize();
Insets i = getInsets();
Graphics cg = g.create();
try {
cg.clipRect(i.left, i.top, vs.width, vs.height);
cg.translate(p.x, p.y);
c.printAll(cg);
} finally {
cg.dispose();
}
}
}
Prints the component in this scroll pane. |
protected void processMouseWheelEvent(MouseWheelEvent e) {
if (isWheelScrollingEnabled()) {
ScrollPaneWheelScroller.handleWheelScrolling(this, e);
e.consume();
}
super.processMouseWheelEvent(e);
}
Process mouse wheel events that are delivered to this
ScrollPane by scrolling an appropriate amount.
Note that if the event parameter is null
the behavior is unspecified and may result in an
exception. |
public final void setLayout(LayoutManager mgr) {
throw new AWTError("ScrollPane controls layout");
}
Sets the layout manager for this container. This method is
overridden to prevent the layout mgr from being set. |
public void setScrollPosition(Point p) {
setScrollPosition(p.x, p.y);
}
Scrolls to the specified position within the child component.
A call to this method is only valid if the scroll pane contains
a child and the specified position is within legal scrolling bounds
of the child. Specifying a position outside of the legal scrolling
bounds of the child will scroll to the closest legal position.
Legal bounds are defined to be the rectangle:
x = 0, y = 0, width = (child width - view port width),
height = (child height - view port height).
This is a convenience method which interfaces with the Adjustable
objects which represent the state of the scrollbars. |
public void setScrollPosition(int x,
int y) {
synchronized (getTreeLock()) {
if (ncomponents < = 0) {
throw new NullPointerException("child is null");
}
hAdjustable.setValue(x);
vAdjustable.setValue(y);
}
}
Scrolls to the specified position within the child component.
A call to this method is only valid if the scroll pane contains
a child. Specifying a position outside of the legal scrolling bounds
of the child will scroll to the closest legal position.
Legal bounds are defined to be the rectangle:
x = 0, y = 0, width = (child width - view port width),
height = (child height - view port height).
This is a convenience method which interfaces with the Adjustable
objects which represent the state of the scrollbars. |
public void setWheelScrollingEnabled(boolean handleWheel) {
wheelScrollingEnabled = handleWheel;
}
Enables/disables scrolling in response to movement of the mouse wheel.
Wheel scrolling is enabled by default. |