+ All Categories
Home > Documents > Bab10-How to Use Scroll Panes

Bab10-How to Use Scroll Panes

Date post: 03-Jun-2018
Category:
Upload: hendi-kurniawanto
View: 219 times
Download: 0 times
Share this document with a friend
16
http://docs.oracle.com/ javase/tutorial/uiswing /components/scro llpane.html How to Use Scroll Panes A JScrollPane  provides a scrollable view of a component. When screen real estate is limited, use a scroll pane to display a component that is large or one whose size can change dynamically. Other containers used to save screen space include split  panes and tabbed panes. The code to create a scroll pane can be minimal. For example, heres a picture of a demo program that puts a text area in a scroll pane because the text areas size grows dynamically as text is appended to it! "eres the code that creates the text area, ma#es it the scroll panes client, and adds the scroll pane to a container! //In a container that uses a BorderLayout: textArea = new JTextArea(5, 30); ... JScrollPane scrollPane = new JScrollPane(textArea); ... setPreferredSize(new Diension(!50, ""0)); ... add(scrollPane, #order$a%o&t.'T*); The boldface line of code creates the JScrollPane, specifying the text area as the scroll panes client. The program doesnt invo#e any methods on the JScrollPane  ob$ect, since the scroll pane handles everything automatically! creating the scroll bars when necessary, redrawing the client when the user moves the scroll #nobs, and so on. %ou might have noticed that the preceding code sets the preferred size of the scroll  panes container. &n the 'ava loo# and feel, this preferred size happens to be a bit less tall than re(uired for the text area to display the ) rows that we re(uested when creating it, so the scroll bar initially displays a vertical scroll bar. &f we didnt restrict the size of the scroll panes container, the scroll pane would be big enough for the text area to display the full ) rows and *+ columns specified with the JTextArea  
Transcript
Page 1: Bab10-How to Use Scroll Panes

8/12/2019 Bab10-How to Use Scroll Panes

http://slidepdf.com/reader/full/bab10-how-to-use-scroll-panes 1/16

http://docs.oracle.com/javase/tutorial/uiswing/components/scrollpane.html

How to Use Scroll PanesA JScrollPane provides a scrollable view of a component. When screen real estate

is limited, use a scroll pane to display a component that is large or one whose size

can change dynamically. Other containers used to save screen space include split

 panes and tabbed panes.

The code to create a scroll pane can be minimal. For example, heres a picture of a

demo program that puts a text area in a scroll pane because the text areas size grows

dynamically as text is appended to it!

"eres the code that creates the text area, ma#es it the scroll panes client, and addsthe scroll pane to a container!

//In a container that uses a BorderLayout:

textArea = new JTextArea(5, 30);...JScrollPane scrollPane = new JScrollPane(textArea);...setPreferredSize(new Diension(!50, ""0));...add(scrollPane, #order$a%o&t.'T*);

The boldface line of code creates the JScrollPane, specifying the text area as thescroll panes client. The program doesnt invo#e any methods on the JScrollPane 

ob$ect, since the scroll pane handles everything automatically! creating the scroll bars

when necessary, redrawing the client when the user moves the scroll #nobs, and so

on.

%ou might have noticed that the preceding code sets the preferred size of the scroll

 panes container. &n the 'ava loo# and feel, this preferred size happens to be a bit less

tall than re(uired for the text area to display the ) rows that we re(uested when

creating it, so the scroll bar initially displays a vertical scroll bar. &f we didnt restrict

the size of the scroll panes container, the scroll pane would be big enough for the

text area to display the full ) rows and *+ columns specified with the JTextArea 

Page 2: Bab10-How to Use Scroll Panes

8/12/2019 Bab10-How to Use Scroll Panes

http://slidepdf.com/reader/full/bab10-how-to-use-scroll-panes 2/16

constructor. efer to -izing a -croll ane for information about techni(ues for

ma#ing a scroll pane the size you want.

The rest of this section discusses the following topics!

• "ow a -croll ane Wor#s

• -etting the -croll /ar olicy

• roviding 0ustom 1ecorations

• &mplementing a -crolling2-avvy 0lient

• -izing a -croll ane

• 1ynamically 0hanging the 0lients -ize

• The -croll ane A&

• 3xamples that 4se -croll anes

How a Scroll Pane Works"ere is a snapshot of an application that uses a customized scroll pane to view a

 photograph!

The scroll pane in this application loo#s very different from the one in the previous

demo program. ather than displaying text, this scroll pane contains a image. The

scroll pane also has two scroll bars, a row header, a column header, and four corners,

three of which have been customized.

Try this:: 

5. 0lic# the 6aunch button to run -croll1emo using 'ava7 Web -tart 

8download '19 : or later ;. Alternatively, to compile and run the example

yourself, consult the example index.

Page 3: Bab10-How to Use Scroll Panes

8/12/2019 Bab10-How to Use Scroll Panes

http://slidepdf.com/reader/full/bab10-how-to-use-scroll-panes 3/16

Page 4: Bab10-How to Use Scroll Panes

8/12/2019 Bab10-How to Use Scroll Panes

http://slidepdf.com/reader/full/bab10-how-to-use-scroll-panes 4/16

A scroll pane uses a J/iew+ort instance to manage the visible area of the client. The

viewport is responsible for positioning and sizing the client, based on the positions ofthe scroll bars, and displaying it.

A scroll pane may use two separate instances of JScroll#ar for the scroll bars. The

scroll bars provide the interface for the user to manipulate the visible area. The

following figure shows the three areas of a scroll bar! the #nob 8sometimes called the

thumb;, the 8arrow; buttons, and the trac#.

When the user moves the #nob on the vertical scroll bar up and down, the visible

area of the client moves up and down. -imilarly, when the user moves the #nob on

the horizontal scroll bar to the right and left, the visible area of the client moves bac#

and forth accordingly. The position of the #nob relative to its trac# is proportionally

e(ual to the position of the visible area relative to the client. &n the 'ava loo# and feel

Page 5: Bab10-How to Use Scroll Panes

8/12/2019 Bab10-How to Use Scroll Panes

http://slidepdf.com/reader/full/bab10-how-to-use-scroll-panes 5/16

and some others, the size of the #nob gives a visual clue as to how much of the client

is visible.

/y clic#ing a arrow button, the user can scroll by a unit increment . /y clic#ing

within the trac#, the user can scroll by a block increment . &f the user has a mousewith a wheel, then the user can scroll vertically using the mouse wheel. The amount

that the mouse wheel scrolls is platform dependent. For example, by default on

Windows , the mouse wheel scrolls three unit incrementsB the =ouse control

 panel allows you to specify a different number of unit increments or to use a bloc#

increment instead. =ore information about unit and bloc# increments is in

&mplementing a -crolling2-avvy 0lient.

Typical programs dont directly instantiate or call methods on a viewport or scroll

 bar. &nstead, programs achieve their scrolling behavior using the JScrollPane A&

and the A& discussed in &mplementing a -crolling2-avvy 0lient. -ome scrolling2

savvy components such as J$ist, JTale, and JTree also provide additional A& tohelp you affect their scrolling behavior.

Setting the Scroll ar Policy

On startup, the scroll pane in the ScrollDeo application has two scroll bars. &f you

ma#e the window large, both scroll bars disappear because they are no longer

needed. &f you then shrin# the height of the window without changing its width, the

vertical scroll bar reappears. Further experimentation will show that in this

application both scroll bars disappear and reappear as needed. This behavior is

controlled by the scroll panes scroll bar policy, Actually, its two policies! eachscroll bar has its own.

ScrollDeo doesnt explicitly set the scroll panes scroll bar policies C it uses the

default. %ou can set the policies when you create the scroll pane or change them

dynamically.

Of the constructors provided by JScrollPane, these two let you set the scroll bar

 policies when you create the scroll pane!

JScrollPane('o+onent, int, int)

JScrollPane(int, int)

The first int specifies the policy for the vertical scroll barB the second specifies the

 policy for the horizontal scroll bar. %ou can also set the policies dynamically with

the set1orizontalScroll#arPolic%  and set/erticalScroll#arPolic%  

methods. With both the constructors and the methods, use one of the following

constants defined in the ScrollPane'onstants  interface 8which is implemented by

JScrollPane;!

Policy !escription/*T2'A$S'*4$$#A*ASDD14*24TA$S'*4$$#A*ASDD The default. The scroll bar appears when the

Page 6: Bab10-How to Use Scroll Panes

8/12/2019 Bab10-How to Use Scroll Panes

http://slidepdf.com/reader/full/bab10-how-to-use-scroll-panes 6/16

viewport is smaller than the client and

disappears when the viewport is larger than the

client./*T2'A$S'*4$$#A*A$6A7S

14*24TA$S'*4$$#A*A$6A7S

Always display the scroll bar. The #nob

disappears if the viewport is large enough toshow the whole client.

/*T2'A$S'*4$$#A*/*14*24TA$S'*4$$#A*/*

 @ever display the scroll bar. 4se this option if

you dont want the user to directly control what

 part of the client is shown, or if you want them

to use only non2scroll2bar techni(ues 8such as

dragging;.

Providing "ustom !ecorations

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. /esides 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 ScrollDeo has custom row and column

headers. Additionally, because all four sides are populated, all four corners are

 present. The program customizes three of the corners C two $ust fill their area with

the same color as the *&les, and the other contains a toggle button. The fourth

corner, the lower right corner, is the default provided by the scroll pane. @otice that

 because the row and column headers are always present in this example, the toggle

 button is also always present.

&f a corner contains a control that the user needs access to all the time, ma#e sure the

sides that intersect at the corner are always present. For example, if this application

Page 7: Bab10-How to Use Scroll Panes

8/12/2019 Bab10-How to Use Scroll Panes

http://slidepdf.com/reader/full/bab10-how-to-use-scroll-panes 7/16

 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 panes row and column headers are provided by a custom J'o+onent subclass, *&le, that draws a ruler in centimeters or inches. "eres the code that

creates and sets the scroll panes row and column headers!

//Where the member variables are defined:

+ri-ate *&le col&n/iew;+ri-ate *&le row/iew;...//Where the GUI is initialized:

2ae2con ee = create2ae2con(8iaesfl%in#ee.9+8);...'reate t:e row and col&n :eaders.

col&n/iew = new *&le(*&le.14*24TA$, tr&e);row/iew = new *&le(*&le./*T2'A$, tr&e);

... pictureScrollPane.setColumnHeaderView(columnView); pictureScrollPane.setRowHeaderView(rowView);

%ou can use any component for a scroll panes row and column headers. The scroll

 pane puts the row and column headers in J/iewPorts of their own. Thus, when

scrolling horizontally, the column header follows along, and when scrolling

vertically, the row header follows along. =a#e sure the row and column have the

same width and height as the view, because '-crollane does not enforce these

values to have the same size. &f one differs from the other, you are li#ely to not getthe desired behavior.

As a J'o+onent subclass, our custom *&le class puts its rendering code in its

+aint'o+onent method. The *&le rendering code ta#es care to draw only within

the current clipping bounds, to ensure speedy scrolling. %our custom row and

column headers should do the same.

%ou can also use any component for the corners of a scroll pane. ScrollDeo 

illustrates this by putting a toggle button in the upper left corner, and custom 'orner 

ob$ects in the upper right and lower left corners. "eres the code that creates the

'orner ob$ects and calls set'orner to place them!

'reate t:e corners.JPanel &tton'orner = new JPanel(); &se low$a%o&tis<etric = new JTole#&tton(8c8, tr&e);is<etric.setont(new ont(8SansSerif8, ont.P$A2, ""));is<etric.set<arin(new 2nsets(,,,));is<etric.add2te$istener(t:is);&tton'orner.add(is<etric);...Set t:e corners.

 pictureScrollPane.setCorner(JScrollPane.UPPR!"#$!C%R&R'

  uttonCorner);

Page 8: Bab10-How to Use Scroll Panes

8/12/2019 Bab10-How to Use Scroll Panes

http://slidepdf.com/reader/full/bab10-how-to-use-scroll-panes 8/16

 pictureScrollPane.setCorner(JScrollPane."%R!"#$!C%R&R'  new Corner());

 pictureScrollPane.setCorner(JScrollPane.UPPR!R*+H$!C%R&R'  new Corner());

emember that the size of each corner is determined by the size of the sidesintersecting there. For some components you must ta#e 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. &ts not an issue with the 'orner class 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 ScrollPane'onstants  interface, which

JScrollPane implements.

#mplementing a Scrolling$Savvy "lient

To customize the way that a client component interacts with its scroll pane, you can

ma#e the component implement the Scrollale interface. /y implementing

Scrollale, a client can specify both the size of the viewport used to view it and the

amount to scroll for clic#s on the different controls on a scroll bar. %ou can also

specify if the view should trac# 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.

%ote: &f you cant or dont want to implement a scrollable client, you can specify the

unit and bloc# increments using the set>nit2ncreent  and set#loc?2ncreent methods of JScroll#ar. For example, the following code sets the unit increment for

vertical scrolling to 5+ pixels!scrollPane.et/erticalScroll#ar().set>nit2ncreent("0);

"ere again are the three control areas of a scroll bar! the #nob, the buttons, and the

trac#.

Page 9: Bab10-How to Use Scroll Panes

8/12/2019 Bab10-How to Use Scroll Panes

http://slidepdf.com/reader/full/bab10-how-to-use-scroll-panes 9/16

%ou might have noticed when manipulating the scroll bars in ScrollDeo that

clic#ing the buttons scrolls the image to a tic# boundary. %ou might also have

noticed that clic#ing in the trac# scrolls the picture by a DscreenfulD. =ore generally,

the button scrolls the visible area by a unit increment and the trac# scrolls the visible

area by a bloc# increment. The behavior you see in the example is not the scroll

 panes default behavior, but is specified by the client in its implementation of the

Scrollale interface.

The client for the ScrollDeo program is ScrollalePict&re.

ScrollalePict&re is a subclass of J$ael that provides implementations of all

five Scrollale methods!

• etScrollale#loc?2ncreent

• etScrollale>nit2ncreent

• etPreferredScrollale/iew+ortSize

• etScrollaleTrac?s/iew+ort1ei:t

• etScrollaleTrac?s/iew+ort6idt:

ScrollalePict&re implements the Scrollale interface primarily to affect the

unit and bloc# increments. "owever, 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 scrolling2savvy classes.

The scroll pane calls the clients etScrollale>nit2ncreent  method whenever

the user clic#s one of the buttons on the scroll bar. This is true as long as the client

implements -crollable. This method returns the number of pixels to scroll. An

obvious implementation of this method returns the number of pixels between tic#

mar#s on the header rulers. ScrollalePict&re, however, does something

different! &t returns the value re(uired to position the image on a tic# mar# boundary.

"eres the implementation!

+&lic int etScrollale>nit2ncreent(*ectanle -isile*ect,  int orientation,  int direction) @  et t:e c&rrent +osition.  int c&rrentPosition = 0;  if (orientation == Swin'onstants.14*24TA$) @  c&rrentPosition = -isile*ect.x;  B else @  c&rrentPosition = -isile*ect.%;

  B

Page 10: Bab10-How to Use Scroll Panes

8/12/2019 Bab10-How to Use Scroll Panes

http://slidepdf.com/reader/full/bab10-how-to-use-scroll-panes 10/16

  *et&rn t:e n&er of +ixels etween c&rrentPosition  and t:e nearest tic? ar? in t:e indicated direction.  if (direction C 0) @  int newPosition = c&rrentPosition

  (c&rrentPosition ax>nit2ncreent)  E ax>nit2ncreent;  ret&rn (newPosition == 0) F ax>nit2ncreent G newPosition;  B else @  ret&rn ((c&rrentPosition ax>nit2ncreent) H ")  E ax>nit2ncreent  c&rrentPosition;  BB

&f the image is already on a tic# mar# boundary, this method returns the number of

 pixels between tic#s. Otherwise, it returns the number of pixels from the current

location to the nearest tic#.

6i#ewise, the scroll pane calls the clients etScrollale#loc?2ncreent  method

each time the user clic#s on the trac#, but only if the client implements -crollable.

"eres ScrollalePict&re s implementation of this method!

+&lic int etScrollale#loc?2ncreent(*ectanle -isile*ect,  int orientation,  int direction) @  if (orientation == Swin'onstants.14*24TA$)  ret&rn -isile*ect.widt: ax>nit2ncreent;  else

  ret&rn -isile*ect.:ei:t ax>nit2ncreent;B

This method returns the height of the visible rectangle minus a tic# mar#. This

 behavior is typical, but true if scrolling vertically, otherwise, its the width.A bloc#

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 8depending on the scroll

direction;.

ScrollalePict&re.9a-a  has one more bit of code thats not re(uired by the

Scrollale interface, but is common in scrollable components! a mouse motionlistener that lets the user scroll the picture by dragging from it. The boldface code in

the following snippet implements scrolling by dragging!

+&lic class ScrollalePict&re extends J$ael  i+leents Scrollale,   ,ouse,otion"istener @  ...  +&lic ScrollalePict&re(...) @  ...  setAutoscrolls(true); enale s%nt:etic dra e-ents  add,ouse,otion"istener(t-is); :andle o&se dras

  B  ...

Page 11: Bab10-How to Use Scroll Panes

8/12/2019 Bab10-How to Use Scroll Panes

http://slidepdf.com/reader/full/bab10-how-to-use-scroll-panes 11/16

   pulic oid mouse/ra00ed(,ouseent e) 1  T:e &ser is drain &s, so scrollI  Rectan0le r = new Rectan0le(e.0et2()' e.0et3()' 4' 4);  scrollRect$oVisile(r);  5

...B

This snippet scrolls the picture whenever the user drags from the picture to a location

outside the picture and pauses. The setA&toscrolls method is defined by

J'o+onent for the purpose of assisting C but not implementing C scrolling by

dragging. -etting the autoscrolls property to tr&e ma#es the component fire

synthetic mouse2dragged events even when the mouse isnt moving 8because it

stopped, mid2drag, outside the component;. &ts up to the components mouse motion

listener to listen for these events and react accordingly.

Si&ing a Scroll Pane

4nless you explicitly set a scroll panes preferred size, the scroll pane computes it

 based on the preferred size of its nine components 8the 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.

&f the client is not scrolling2savvy, then the scroll pane sizes itself so that the client

displays at its preferred size. For typical unsavvy clients, this ma#es the scroll pane

redundant. That is, the scroll pane has no scroll bars because the clients preferredsize is big enough to display the entire client. &n this case, if the client doesnt 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.

&f the client is scrolling2savvy, then the scroll pane uses the value returned by the

clients etPreferredScrollale/iew+ortSize  method to compute the size of its

viewport. &mplementations of this method generally report a preferred size for

scrolling thats smaller than the components standard preferred size. For example, by

default, the value returned by J$ists implementation of

etPreferredScrollale/iew+ortSize  is $ust big enough to display eight rows.

-crolling2savvy classes, li#e lists, tables, text components, and trees, often provide

one or more methods that let programmers affect the size returned from

etPreferredScrollale/iew+ortSize . For example, you can set the number of

visible rows in a list or a tree by calling the set/isile*ow'o&nt  method. The list

or tree ta#es care of figuring out the size needed to display that number of rows.

efer to =ethods in Other 0lasses elated to -crolling for information about

scrolling2related methods provided by classes other than JScrollPane. And

remember C if you dont li#e the value that

etPreferredScrollale/iew+ortSize  returns, you can always set the preferred

size of the scroll pane or its container.

Page 12: Bab10-How to Use Scroll Panes

8/12/2019 Bab10-How to Use Scroll Panes

http://slidepdf.com/reader/full/bab10-how-to-use-scroll-panes 12/16

!ynamically "hanging the "lient's Si&e

0hanging the size of a scroll panes client is a two2step process. First, set the clients

 preferred size. Then, call re-alidate on the client to let the scroll pane #now that it

should update itself and its scroll bars. 6ets loo# at an example.

"eres a picture of an application that changes the clients size whenever the user

 places a circle whose bounds fall outside of the clients current bounds. The program

also changes the clients size when the user clears the drawing area!

%ou can find the full source code for this example in ScrollDeo.9a-a , which is

 based on an example provided by tutorial reader 'ohn Eella. %ou can run

Scroll!emo( 8 download '19 : or later ;.

"eres the code that changes the drawing areas size when necessary!

if (c:aned) @  >+date clients +referred size eca&se  t:e area ta?en &+ % t:e ra+:ics :as  otten larer or saller (if cleared).  drawinArea.setPreferredSize(/* the new size */ );

  $et t:e scroll +ane ?now to &+date itself  and its scroll ars.  drawinArea.re-alidate();B

 @ote that when the client changes size, the scroll bars ad$ust. The scroll pane doesnt

resize, nor does the viewport.

efer to S+litPaneDeo for another example in which the client ob$ect changes size.

The Scroll Pane )P#

Page 13: Bab10-How to Use Scroll Panes

8/12/2019 Bab10-How to Use Scroll Panes

http://slidepdf.com/reader/full/bab10-how-to-use-scroll-panes 13/16

The following tables list the commonly used scroll2related constructors and methods.

Other methods you are most li#ely to invo#e on a JScrollPane ob$ect are those such

as setPreferredSize that its superclasses provide. -ee The '0omponent A& for

tables of commonly used inherited methods.

The A& for using scroll panes falls into these categories!

• -etting 4p the -croll ane

• 1ecorating the -croll ane

• &mplementing a -crolling2-avvy 0lient

• =ethods in Other 0lasses elated to -crolling

-etting 4p the -croll ane

8JScrollPane constructors and methods;

*ethod or "onstructor Purpose

'-crollane8;

'-crollane80omponent;

'-crollane8int, int;

'-crollane80omponent, int, int;

0reate a scroll pane. The 'o+onent 

 parameter, when present, sets the scroll panes

client. The two int parameters, when present,

set the vertical and horizontal scroll bar

 policies 8respectively;.

-oid set/iew+ort/iew('o+onent) -et the scroll panes client.

-oidset/erticalScroll#arPolic%(int)

intet/erticalScroll#arPolic%()

-et or get the vertical scroll policy.

ScrollPane'onstants  defines three values

for specifying this policy!

/*T2'A$S'*4$$#A*ASDD  8thedefault;, /*T2'A$S'*4$$#A*A$6A7S , and

/*T2'A$S'*4$$#A*/* .

void set"orizontal-croll/arolicy8int;

int get"orizontal-croll/arolicy8;

-et or get the horizontal scroll policy.

ScrollPane'onstants  defines three values

for specifying this policy!

14*24TA$S'*4$$#A*ASDD  8the

default;, 14*24TA$S'*4$$#A*A$6A7S ,

and 14*24TA$S'*4$$#A*/* .

void setEiewport/order8/order;/order getEiewport/order8;

-et or get the border around the viewport.This

is preferred over setting the border on thecomponent.

 boolean isWheel-crolling3nabled8;

-et or get whether scrolling occurs in

response to the mouse wheel. =ouse2wheel

scrolling is enabled by default.

1ecorating the -croll ane

8JScrollPane methods;

*ethod Purpose

void

set0olumn"eaderEiew80omponent;

-et the column or row header for the scroll

 pane.

Page 14: Bab10-How to Use Scroll Panes

8/12/2019 Bab10-How to Use Scroll Panes

http://slidepdf.com/reader/full/bab10-how-to-use-scroll-panes 14/16

void

setow"eaderEiew80omponent;

void set0orner8-tring, 0omponent;

0omponent get0orner8-tring;

-et or get the corner specified. The int 

 parameter specifies which corner and must beone of the following constants defined in

ScrollPane'onstants! >PP*$T'4**,

>PP**21T'4** , $46*$T'4**,

$46**21T'4** ,

$46*$AD2'4** ,

$46*T*A2$2'4**,

>PP*$AD2'4** , and

>PP*T*A2$2'4**.

&mplementing a -crolling2-avvy 0lient

*ethod Purpose

int

get-crollable4nit&ncrement8ectangle,

int, int;

int

get-crollable/loc#&ncrement8ectangle,

int, int;

(required by the crollable interface)

et the unit or bloc# increment in pixels.

The *ectanle parameter is the bounds of

the currently visible rectangle. The first int

 parameter is either

Swin'onstants.14*24TA$  or

Swin'onstants./*T2'A$ depending on

what scroll bar the user clic#ed on. The

second int parameter indicates which

direction to scroll. A value less than +

indicates up or left. A value greater than +

indicates down or right.

1imension

getreferred-crollableEiewport-ize8;

(required by the crollable interface)

et the preferred size of the viewport. This

allows the client to influence the size of the

viewport in which it is displayed. &f the

viewport size is unimportant, implement

this method to return etPreferredSize .

 boolean

get-crollableTrac#sEiewportWidth8;

 boolean

get-crollableTrac#sEiewport"eight8;(required by the crollable interface)

et whether the scroll pane should force

the client to be the same width or height as

the viewport. A return value of tr&e from

either of these methods effectively

disallows horizontal or vertical scrolling

8respectively;.

void setAutoscrolls8boolean;

(in !"om#onent )

-et whether synthetic mouse dragged

events should be generated when the user

drags the mouse outside of the component

and stopsB these events are necessary for

scrolling by dragging. /y default, the value

is false, but many scrollable components

such as JTale and custom components set

the value to tr&e.

=ethods in Other 0lasses elated to -crolling

Page 15: Bab10-How to Use Scroll Panes

8/12/2019 Bab10-How to Use Scroll Panes

http://slidepdf.com/reader/full/bab10-how-to-use-scroll-panes 15/16

*ethod Purpose

void scrollectToEisible8ectangle;

(in !"om#onent )

&f the component is in a container that

supports scrolling, such as a scroll pane,

then calling this method scrolls thescroll pane such that the specified

rectangle is visible.

void setEisibleow0ount8int;

int getEisibleow0ount8;

(in !List )

-et or get how many rows of the list are

visible. TheetPreferredScrollale/iew+ortSi

ze method uses the visible row count to

compute its return value.

void ensure&ndex&sEisible8int;

(in !List )

-croll so that the row at the specified

index is visible. This method calls

scroll*ectTo/isile and wor#s only

if the list is in a container, such as a

scroll pane, that supports scrolling.

void setEisibleow0ount8int;

int getEisibleow0ount8;

(in !$ree )

-et or get how many rows of the tree are

visible. TheetPreferredScrollale/iew+ortSi

ze method uses the visible row count to

compute its return value.

void scrollathToEisible8Treeath;

void scrollowToEisible8int;(in !$ree )

-croll so that the specified tree path or

row at the specified index is visible.

These methods call

scroll*ectTo/isile and wor# onlyif the tree is in a container, such as a

scroll pane, that supports scrolling.

void set-crollsOn3xpand8boolean;

 boolean get-crollsOn3xpand8;

(in !$ree )

-et or get whether scrolling occurs

automatically when the user expands a

node. True by default. This feature

wor#s only when the tree is in a

container, such as a scroll pane, that

supports scrolling.

void

setreferred-crollableEiewport-ize81imension;

(in !$able )

-et the value to be returned byetPreferredScrollale/iew+ortSi

ze.

+,amples that Use Scroll Panes

This table shows the examples that use JScrollPane and where those examples are

described.

+,ample

Where

!escri-ed %otes

Page 16: Bab10-How to Use Scroll Panes

8/12/2019 Bab10-How to Use Scroll Panes

http://slidepdf.com/reader/full/bab10-how-to-use-scroll-panes 16/16

Tool#arDeo

This section,

"ow to 4se Tool 

/ars

-hows a simple, yet typical, use of a scroll pane.

ScrollDeo This section 4ses many of scroll panes bells and whistles.

ScrollDeo This section -hows how to change the clients size.

S+litPaneDeo

"ow to 4se -plit 

anes,

"ow to 4se

6ists

uts a list and a label in a scroll pane. Also,

shows how to handle the case when a scroll

 panes client changes size.

TaleDeo"ow to 4se

Tablesuts a table in a scroll pane.

TextSa+lerDeo4sing Text

0omponents

uts a text area, an editor pane, and a text pane

each in a scroll pane.

TreeDeo"ow to 4se

Treesuts a tree in a scroll pane.


Recommended