![]() |
||
![]() |
![]() Alden Hosting provides professional, efficient, and reliable business-class Web hosting services to small- and medium-sized businesses. |
|
|
JAVA, JSP, SERVLETS, TOMCAT, SERVLETS MANAGER, |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Policy | Description |
|---|---|
VERTICAL_SCROLLBAR_AS_NEEDED
HORIZONTAL_SCROLLBAR_AS_NEEDED
|
The default. The scroll bar appears when the viewport is smaller than the client and disappears when the viewport is larger than the client. |
VERTICAL_SCROLLBAR_ALWAYS
HORIZONTAL_SCROLLBAR_ALWAYS
|
Always display the scroll bar. The knob disappears if the viewport is large enough to show the whole client. |
VERTICAL_SCROLLBAR_NEVER
HORIZONTAL_SCROLLBAR_NEVER
|
Never display the scroll bar. Use this option if you don't want the user to directly control what part of the client is shown, or if you want them to use only non-scroll-bar techniques (such as dragging). |
The area drawn by a scroll pane consists of up to nine parts: the center, four sides, and four corners. The center is the only component that is always present in all scroll panes. Besides scroll bars, the sides can contain column and row headers. A corner component is visible only if both sides that intersect at that corner contain visible components.As shown in the figure, the scroll pane in
ScrollDemohas custom row and column headers. Additionally, because all four sides are populated, all four corners are present. The program customizes three of the corners — two just fill their area with the same color as theRules, and the other contains a toggle button. The fourth corner, the lower right corner, is the default provided by the scroll pane. Notice that because the row and column headers are always present in this example, the toggle button is also always present.If a corner contains a control that the user needs access to all the time, make sure the sides that intersect at the corner are always present. For example, if this application placed the toggle in the lower right corner where the scroll bars intersect, then the toggle would disappear if the user resized the window and even one of the scroll bars disappeared.
The scroll pane's row and column headers are provided by a custom
JComponentsubclass,Rule, that draws a ruler in centimeters or inches. Here's the code that creates and sets the scroll pane's row and column headers:You can use any component for a scroll pane's row and column headers. The scroll pane puts the row and column headers in//Where the member variables are defined: private Rule columnView; private Rule rowView; ... //Where the GUI is initialized: ImageIcon david = createImageIcon("images/youngdad.jpeg"); ... //Create the row and column headers. columnView = new Rule(Rule.HORIZONTAL, true); rowView = new Rule(Rule.VERTICAL, true); ... pictureScrollPane.setColumnHeaderView(columnView); pictureScrollPane.setRowHeaderView(rowView);JViewPorts of their own. Thus, when scrolling horizontally, the column header follows along, and when scrolling vertically, the row header follows along. Make sure the row and column have the same width and height as the view, because JScrollPane does not enforce these values to have the same size. If one differs from the other, you are likely to not get the desired behavior.As a
JComponentsubclass, our customRuleclass puts its rendering code in itspaintComponentmethod. TheRulerendering code takes care to draw only within the current clipping bounds, to ensure speedy scrolling. Your custom row and column headers should do the same.You can also use any component for the corners of a scroll pane.
ScrollDemoillustrates this by putting a toggle button in the upper left corner, and customCornerobjects in the upper right and lower left corners. Here's the code that creates theCornerobjects and callssetCornerto place them://Create the corners. JPanel buttonCorner = new JPanel(); //use FlowLayout isMetric = new JToggleButton("cm", true); isMetric.setFont(new Font("SansSerif", Font.PLAIN, 11)); isMetric.setMargin(new Insets(2,2,2,2)); isMetric.addItemListener(this); buttonCorner.add(isMetric); ... //Set the corners. pictureScrollPane.setCorner(JScrollPane.UPPER_LEFT_CORNER, buttonCorner); pictureScrollPane.setCorner(JScrollPane.LOWER_LEFT_CORNER, new Corner()); pictureScrollPane.setCorner(JScrollPane.UPPER_RIGHT_CORNER, new Corner());Remember that the size of each corner is determined by the size of the sides intersecting there. For some components you must take care that the specific instance of the component fits in its corner. For example, the program sets the font and margins on the toggle button so that it fits within the space established by the headers. It's not an issue with the
Cornerclass because that class colors its entire bounds, whatever they happen to be, with a solid color.As you can see from the code, constants indicate the corner positions. This figure shows the constant for each position:
The constants are defined in the ScrollPaneConstantsinterface, whichJScrollPaneimplements.
To customize the way that a client component interacts with its scroll pane, you can make the component implement theScrollableinterface. By implementingScrollable, a client can specify both the size of the viewport used to view it and the amount to scroll for clicks on the different controls on a scroll bar. You can also specify if the view should track the size of the viewport. This is typically used when the viewport is bigger than the view, but the view should fill the available space.
Note: If you can't or don't want to implement a scrollable client, you can specify the unit and block increments using thesetUnitIncrementandsetBlockIncrementmethods ofJScrollBar. For example, the following code sets the unit increment for vertical scrolling to 10 pixels:scrollPane.getVerticalScrollBar().setUnitIncrement(10);Here again are the three control areas of a scroll bar: the knob, the buttons, and the track.
You might have noticed when manipulating the scroll bars in ScrollDemothat clicking the buttons scrolls the image to a tick boundary. You might also have noticed that clicking in the track scrolls the picture by a "screenful". More generally, the button scrolls the visible area by a unit increment and the track scrolls the visible area by a block increment. The behavior you see in the example is not the scroll pane's default behavior, but is specified by the client in its implementation of theScrollableinterface.The client for the
ScrollDemoprogram isScrollablePicture.ScrollablePictureis a subclass ofJLabelthat provides implementations of all fiveScrollablemethods:
getScrollableBlockIncrementgetScrollableUnitIncrementgetPreferredScrollableViewportSizegetScrollableTracksViewportHeightgetScrollableTracksViewportWidthScrollablePictureimplements theScrollableinterface primarily to affect the unit and block increments. However, it must provide implementations for all five methods. Thus, it provides reasonable defaults for the other three methods that you might want to copy for your scrolling-savvy classes.The scroll pane calls the client's
getScrollableUnitIncrementmethod whenever the user clicks one of the buttons on the scroll bar. This is true as long as the client implements Scrollable. This method returns the number of pixels to scroll. An obvious implementation of this method returns the number of pixels between tick marks on the header rulers.ScrollablePicture, however, does something different: It returns the value required to position the image on a tick mark boundary. Here's the implementation:If the image is already on a tick mark boundary, this method returns the number of pixels between ticks. Otherwise, it returns the number of pixels from the current location to the nearest tick.public int getScrollableUnitIncrement(Rectangle visibleRect, int orientation, int direction) { //Get the current position. int currentPosition = 0; if (orientation == SwingConstants.HORIZONTAL) { currentPosition = visibleRect.x; } else { currentPosition = visibleRect.y; } //Return the number of pixels between currentPosition //and the nearest tick mark in the indicated direction. if (direction < 0) { int newPosition = currentPosition - (currentPosition / maxUnitIncrement) * maxUnitIncrement; return (newPosition == 0) ? maxUnitIncrement : newPosition; } else { return ((currentPosition / maxUnitIncrement) + 1) * maxUnitIncrement - currentPosition; } }Likewise, the scroll pane calls the client's
getScrollableBlockIncrementmethod each time the user clicks on the track, but only if the client implements Scrollable. Here'sScrollablePicture's implementation of this method:This method returns the height of the visible rectangle minus a tick mark. This behavior is typical, but true if scrolling vertically, otherwise, it's the width.A block increment should be slightly smaller than the viewport to leave a little of the previous visible area for context. For example, a text area might leave one or two lines of text for context and a table might leave a row or column (depending on the scroll direction).public int getScrollableBlockIncrement(Rectangle visibleRect, int orientation, int direction) { if (orientation == SwingConstants.HORIZONTAL) return visibleRect.width - maxUnitIncrement; else return visibleRect.height - maxUnitIncrement; }
ScrollablePicture.javahas one more bit of code that's not required by theScrollableinterface, but is common in scrollable components: a mouse motion listener that lets the user scroll the picture by dragging from it. The boldface code in the following snippet implements scrolling by dragging:This snippet scrolls the picture whenever the user drags from the picture to a location outside the picture and pauses. Thepublic class ScrollablePicture extends JLabel implements Scrollable, MouseMotionListener { ... public ScrollablePicture(...) { ... setAutoscrolls(true); //enable synthetic drag events addMouseMotionListener(this); //handle mouse drags } ... public void mouseDragged(MouseEvent e) { //The user is dragging us, so scroll! Rectangle r = new Rectangle(e.getX(), e.getY(), 1, 1); scrollRectToVisible(r); } ... }setAutoscrollsmethod is defined byJComponentfor the purpose of assisting — but not implementing — scrolling by dragging. Setting the autoscrolls property totruemakes the component fire synthetic mouse-dragged events even when the mouse isn't moving (because it stopped, mid-drag, outside the component). It's up to the component's mouse motion listener to listen for these events and react accordingly.
Unless you explicitly set a scroll pane's preferred size, the scroll pane computes it based on the preferred size of its nine components (the viewport, and, if present, the two scroll bars, the row and column headers, and the four corners). The largest factor, and the one most programmers care about, is the size of the viewport used to display the client.If the client is not scrolling-savvy, then the scroll pane sizes itself so that the client displays at its preferred size. For typical unsavvy clients, this makes the scroll pane redundant. That is, the scroll pane has no scroll bars because the client's preferred size is big enough to display the entire client. In this case, if the client doesn't change size dynamically, you should probably limit the size of the scroll pane by setting its preferred size or the preferred size of its container.
If the client is scrolling-savvy, then the scroll pane uses the value returned by the client's
getPreferredScrollableViewportSizemethod to compute the size of its viewport. Implementations of this method generally report a preferred size for scrolling that's smaller than the component's standard preferred size. For example, by default, the value returned byJList's implementation ofgetPreferredScrollableViewportSizeis just big enough to display eight rows.Scrolling-savvy classes, like lists, tables, text components, and trees, often provide one or more methods that let programmers affect the size returned from
getPreferredScrollableViewportSize. For example, you can set the number of visible rows in a list or a tree by calling thesetVisibleRowCountmethod. The list or tree takes care of figuring out the size needed to display that number of rows.Refer to Methods in Other Classes Related to Scrolling for information about scrolling-related methods provided by classes other than
JScrollPane. And remember — if you don't like the value thatgetPreferredScrollableViewportSizereturns, you can always set the preferred size of the scroll pane or its container.
Changing the size of a scroll pane's client is a two-step process. First, set the client's preferred size. Then, callrevalidateon the client to let the scroll pane know that it should update itself and its scroll bars. Let's look at an example.Here's a picture of an application that changes the client's size whenever the user places a circle whose bounds fall outside of the client's current bounds. The program also changes the client's size when the user clears the drawing area:
You can find the full source code for this example in
ScrollDemo2.java, which is based on an example provided by tutorial reader John Vella. You can run ScrollDemo2 ( download JDK 6).Here's the code that changes the drawing area's size when necessary:
Note that when the client changes size, the scroll bars adjust. The scroll pane doesn't resize, nor does the viewport.if (changed) { //Update client's preferred size because //the area taken up by the graphics has //gotten larger or smaller (if cleared). drawingArea.setPreferredSize(/* the new size */); //Let the scroll pane know to update itself //and its scroll bars. drawingArea.revalidate(); }Refer to
SplitPaneDemofor another example in which the client object changes size.
The following tables list the commonly used scroll-related constructors and methods. Other methods you are most likely to invoke on aJScrollPaneobject are those such assetPreferredSizethat its superclasses provide. See The JComponent API for tables of commonly used inherited methods.The API for using scroll panes falls into these categories:
- Setting Up the Scroll Pane
- Decorating the Scroll Pane
- Implementing a Scrolling-Savvy Client
- Methods in Other Classes Related to Scrolling
Setting Up the Scroll Pane
(JScrollPaneconstructors and methods)Method or Constructor Purpose JScrollPane()
JScrollPane(Component)
JScrollPane(int, int)
JScrollPane(Component, int, int)Create a scroll pane. The Componentparameter, when present, sets the scroll pane's client. The twointparameters, when present, set the vertical and horizontal scroll bar policies (respectively).void setViewportView(Component)Set the scroll pane's client. void setVerticalScrollBarPolicy(int)
int getVerticalScrollBarPolicy()Set or get the vertical scroll policy. ScrollPaneConstantsdefines three values for specifying this policy:VERTICAL_SCROLLBAR_AS_NEEDED(the default),VERTICAL_SCROLLBAR_ALWAYS, andVERTICAL_SCROLLBAR_NEVER.void setHorizontalScrollBarPolicy(int)
int getHorizontalScrollBarPolicy()Set or get the horizontal scroll policy. ScrollPaneConstantsdefines three values for specifying this policy:HORIZONTAL_SCROLLBAR_AS_NEEDED(the default),HORIZONTAL_SCROLLBAR_ALWAYS, andHORIZONTAL_SCROLLBAR_NEVER.void setViewportBorder(Border)
Border getViewportBorder()Set or get the border around the viewport.This is preferred over setting the border on the component. boolean isWheelScrollingEnabled() Set or get whether scrolling occurs in response to the mouse wheel. Mouse-wheel scrolling is enabled by default.
Decorating the Scroll Pane
(JScrollPanemethods)Method Purpose void setColumnHeaderView(Component)
void setRowHeaderView(Component)Set the column or row header for the scroll pane. void setCorner(String, Component)
Component getCorner(String)Set or get the corner specified. The intparameter specifies which corner and must be one of the following constants defined inScrollPaneConstants:UPPER_LEFT_CORNER,UPPER_RIGHT_CORNER,LOWER_LEFT_CORNER,LOWER_RIGHT_CORNER,LOWER_LEADING_CORNER,LOWER_TRAILING_CORNER,UPPER_LEADING_CORNER, andUPPER_TRAILING_CORNER.
Implementing a Scrolling-Savvy Client Method Purpose int getScrollableUnitIncrement(Rectangle, int, int)
int getScrollableBlockIncrement(Rectangle, int, int)
(required by theScrollableinterface)Get the unit or block increment in pixels. The Rectangleparameter is the bounds of the currently visible rectangle. The firstintparameter is eitherSwingConstants.HORIZONTALorSwingConstants.VERTICALdepending on what scroll bar the user clicked on. The secondintparameter indicates which direction to scroll. A value less than 0 indicates up or left. A value greater than 0 indicates down or right.Dimension getPreferredScrollableViewportSize()
(required by theScrollableinterface)Get the preferred size of the viewport. This allows the client to influence the size of the viewport in which it is displayed. If the viewport size is unimportant, implement this method to return getPreferredSize.boolean getScrollableTracksViewportWidth()
boolean getScrollableTracksViewportHeight()
(required by theScrollableinterface)Get whether the scroll pane should force the client to be the same width or height as the viewport. A return value of truefrom either of these methods effectively disallows horizontal or vertical scrolling (respectively).void setAutoscrolls(boolean)
(inJComponent)Set whether synthetic mouse dragged events should be generated when the user drags the mouse outside of the component and stops; these events are necessary for scrolling by dragging. By default, the value is false, but many scrollable components such asJTableand custom components set the value totrue.
Methods in Other Classes Related to Scrolling Method Purpose void scrollRectToVisible(Rectangle)
(inJComponent)If the component is in a container that supports scrolling, such as a scroll pane, then calling this method scrolls the scroll pane such that the specified rectangle is visible. void setVisibleRowCount(int)
int getVisibleRowCount()
(inJList)Set or get how many rows of the list are visible. The getPreferredScrollableViewportSizemethod uses the visible row count to compute its return value.void ensureIndexIsVisible(int)
(inJList)Scroll so that the row at the specified index is visible. This method calls scrollRectToVisibleand works only if the list is in a container, such as a scroll pane, that supports scrolling.void setVisibleRowCount(int)
int getVisibleRowCount()
(inJTree)Set or get how many rows of the tree are visible. The getPreferredScrollableViewportSizemethod uses the visible row count to compute its return value.void scrollPathToVisible(TreePath)
void scrollRowToVisible(int)
(inJTree)Scroll so that the specified tree path or row at the specified index is visible. These methods call scrollRectToVisibleand work only if the tree is in a container, such as a scroll pane, that supports scrolling.void setScrollsOnExpand(boolean)
boolean getScrollsOnExpand()
(inJTree)Set or get whether scrolling occurs automatically when the user expands a node. True by default. This feature works only when the tree is in a container, such as a scroll pane, that supports scrolling. void setPreferredScrollableViewportSize(Dimension)
(inJTable)Set the value to be returned by getPreferredScrollableViewportSize.
This table shows the examples that useJScrollPaneand where those examples are described.
Example Where Described Notes ToolBarDemoThis section,
How to Use Tool BarsShows a simple, yet typical, use of a scroll pane. ScrollDemoThis section Uses many of scroll pane's bells and whistles. ScrollDemo2This section Shows how to change the client's size. SplitPaneDemoHow to Use Split Panes,
How to Use ListsPuts a list and a label in a scroll pane. Also, shows how to handle the case when a scroll pane's client changes size. TableDemoHow to Use Tables Puts a table in a scroll pane. TextSamplerDemoUsing Text Components Puts a text area, an editor pane, and a text pane each in a scroll pane. TreeDemoHow to Use Trees Puts a tree in a scroll pane.
Alden Hosting offers private JVM (Java Virtual Machine), Java Server Pages (JSP), Servlets, and Servlets Manager with our Web Hosting Plans WEB 4 PLAN and WEB 5 PLAN , WEB 6 PLAN .
At Alden Hosting we eat and breathe Java! We are the industry leader in providing affordable, quality and efficient Java web hosting in the shared hosting marketplace. All our sites run on our Java hosing platform configured for optimum performance using Java 1.6, Tomcat 6.0.X, MySQL 5.0.x, Apache 2.2.xx and web application frameworks such as Struts, Hibernate, Cocoon, Ant, etc.
We offer only one type of Java hosting - Private Tomcat. Hosting accounts on the Private Tomcat environment get their very own Tomcat server. You can start and re-start your entire Tomcat server yourself.
![]() |
|
http://alden-servlet-Hosting.com
JSP at alden-servlet-Hosting.com
Servlets at alden-servlet-Hosting.com
Servlet at alden-servlet-Hosting.com
Tomcat at alden-servlet-Hosting.com
MySQL at alden-servlet-Hosting.com
Java at alden-servlet-Hosting.com
sFTP at alden-servlet-Hosting.com
http://alden-tomcat-Hosting.com
JSP at alden-tomcat-Hosting.com
Servlets at alden-tomcat-Hosting.com
Servlet at alden-tomcat-Hosting.com
Tomcat at alden-tomcat-Hosting.com
MySQL at alden-tomcat-Hosting.com
Java at alden-tomcat-Hosting.com
sFTP at alden-tomcat-Hosting.com
http://alden-sftp-Hosting.com
JSP at alden-sftp-Hosting.com
Servlets at alden-sftp-Hosting.com
Servlet at alden-sftp-Hosting.com
Tomcat at alden-sftp-Hosting.com
MySQL at alden-sftp-Hosting.com
Java at alden-sftp-Hosting.com
sFTP at alden-sftp-Hosting.com
http://alden-jsp-Hosting.com
JSP at alden-jsp-Hosting.com
Servlets at alden-jsp-Hosting.com
Servlet at alden-jsp-Hosting.com
Tomcat at alden-jsp-Hosting.com
MySQL at alden-jsp-Hosting.com
Java at alden-jsp-Hosting.com
sFTP at alden-jsp-Hosting.com
http://alden-java-Hosting.com
JSp at alden-java-Hosting.com
Servlets at alden-java-Hosting.com
Servlet at alden-java-Hosting.com
Tomcat at alden-java-Hosting.com
MySQL at alden-java-Hosting.com
Java at alden-java-Hosting.com
sFTP at alden-java-Hosting.com
JSP
Servlets
Tomcat
mysql
Java
JSP
Servlets
Tomcat
mysql
Java
JSP
Servlets
Tomcat
mysql
Java
JSP
Servlets
Tomcat
mysql
Java
JSP at JSP.aldenWEBhosting.com
Servlets at servlets.aldenWEBhosting.com
Tomcat at Tomcat.aldenWEBhosting.com
mysql at mysql.aldenWEBhosting.com
Java at Java.aldenWEBhosting.com
Web Hosts Portal
Web Links
Web Links
Web Hosting
JSP Solutions Web Links
JSP Solutions Web Hosting
Servlets Solutions Web Links
Servlets Solutions Web Hosting
Web Links
Web Links
.
.
.
.
.
.
.
.
.
.
jsp hosting
servlets hosting
web hosting
web sites designed
cheap web hosting
web site hosting
myspace web hosting