Sei sulla pagina 1di 9

http://docs.oracle.com/javase/tutorial/uiswing/components/slider.

html

How to Use Sliders


Komponen JSlider dapat diguankan user untuk memasukkan nilai numeris dengan lebih mudah pada daerah nilai minimum dan maksimumnya.

Menciptakan komponen JSlider


Ada 6 konstruktor untuk JSlider:
public JSlider() JSlider aJSlider = new JSlider(); public JSlider(int orientation) // Vertical JSlider aJSlider = new JSlider(JSlider.VERTICAL); // Horizontal JSlider bJSlider = new JSlider(JSlider.H RI! "TAL); public JSlider(int minimum, int maximum) // Initial #al$e %id&oint / ' JSlider aJSlider = new JSlider(()''* )''); public JSlider(int minimum, int maximum, int value) JSlider aJSlider = new JSlider(()''* )''* '); public JSlider(int orientation, int minimum, int maximum, int value) // Vertical* initial #al$e +* ran,e )()- (%ont./ o0 1ear) JSlider aJSlider = new JSlider(JSlider.VERTICAL* )* )-* +); public JSlider(BoundedRangeModel model) // 2ata %odel* initial #al$e 3* ran,e )(3)* and e4tent o0 ' // JSlider direction c.an,ed to #ertical &rior to di/&la1 on /creen 2e0a$lt5o$ndedRan,e6odel %odel = new 2e0a$lt5o$ndedRan,e6odel(3* '* )* 3)); JSlider aJSlider = new JSlider(%odel); aJSlider./et rientation(JSlider.VERTICAL);

Menciptakan JSlider tanpa argument, maka akan terjadi horizontal slider dengan de ault data model. Model mempunyai initial !alue "#, minimum #, ma$imum %##, dan e$tent #.

JSlider &roperties
Setelah menciptakan JSlider, anda dapat memodi ikasi data dalam model. Anda dapat memperoleh model dengan method public 'ounded(angeModel getModel)*, dan kemudian memodi ikasi secara langsung. Anda juga dapat memanggil secara langsung method dari komponen ini. Memperoleh nilai )!alue*: method get+alue)*,
Strin, #al$e = Strin,.#al$e 0(/lider.,etVal$e()); label./etTe4t(#al$e);

Menset nilai slider: method set+alue)*


/lider./etVal$e(78);

Menset nilai minimum slider: method setMinimum)*


/lider./et6ini%$%(8');

Menset nilai minimum slider: method setMinimum)*


/lider./et6a4i%$%(78);

Menset orientasi slider: method set-rientation)*


/lider./et rientation(9a#a4./win,.JSlider.VERTICAL);

Menampilkan tick: method set&aint.ick)*


/lider./et:aintTic;/(tr$e);

argumen / alse menyembunyikan tick Menampilkan label: method set&aint0abel)*


/lider./et:aintLabel/(tr$e);

argumen / alse menyembuny ikan label Menampilkan track: method set&aint.rack)*


/lider./et:aintTrac;(0al/e);

1ubungan antara 2 range:


%ini%$% <= #al$e <= #al$e=e4tent <= %a4i%$%

Properti JSlider

3ntuk mengakses sejumlah properti ini dapat dikerjakan le4at model:


5o$ndedRan,e6odel %odel = /lider.,et6odel(); %odel./etVal$e(78);

5e inisi inter ace 'ounded(angeModel adalah:


&$blic inter0ace 5o$ndedRan,e6odel > // :ro&ertie/ &$blic int ,etE4tent(); &$blic #oid /etE4tent(int newVal$e); &$blic int ,et6a4i%$%(); &$blic #oid /et6a4i%$%(int newVal$e); &$blic int ,et6ini%$%(); &$blic #oid /et6ini%$%(int newVal$e); &$blic int ,etVal$e(); &$blic #oid /etVal$e(int newVal$e); &$blic boolean ,etVal$eI/Ad9$/tin,(); &$blic #oid /etVal$eI/Ad9$/tin,(boolean newVal$e);

// Li/tener/ &$blic #oid addC.an,eLi/tener(C.an,eLi/tener li/tener); &$blic #oid re%o#eC.an,eLi/tener(C.an,eLi/tener li/tener); // t.er 6et.od/ &$blic #oid /etRan,e:ro&ertie/(int #al$e* int e4tent* int %ini%$%* int %a4i%$%* boolean ad9$/tin,);

Anda dapat melacak perubahan terhadap JSlider dengan 6hange0istener.


/lider.addC.an,eLi/tener(new 9a#a4./win,.e#ent.C.an,eLi/tener() > &$blic #oid /tateC.an,ed(9a#a4./win,.e#ent.C.an,eE#ent e#t) > /liderStateC.an,ed(e#t); ? ?);

.he ollo4ing picture sho4s an application that uses a slider to control animation speed:

Try this: 1. Click the Launch button to run SliderDemo using Java Web Start (download JDK or later!. "lternativel#$ to com%ile and run the e&am%le #oursel'$ consult the e&am%le inde&. (. )se the slider to ad*ust the animation s%eed. +. ,ush the slider to - to sto% the animation.

'elo4 is the code rom the Slider2e%o.9a#a ile that creates the slider in the pre!ious e$ample.
/tatic 0inal int @:SA6I" = '; /tatic 0inal int @:SA6AB = 3'; /tatic 0inal int @:SAI"IT = )8; //initial 0ra%e/ &er /econd . . . JSlider 0ra%e/:erSecond = new JSlider(JSlider.H RI! "TAL* @:SA6I"* @:SA6AB* @:SAI"IT); 0ra%e/:erSecond.addC.an,eLi/tener(t.i/); //T$rn on label/ at %a9or tic; %ar;/. 0ra%e/:erSecond./et6a9orTic;S&acin,()'); 0ra%e/:erSecond./et6inorTic;S&acin,()); 0ra%e/:erSecond./et:aintTic;/(tr$e); 0ra%e/:erSecond./et:aintLabel/(tr$e);

'y de ault, spacing or major and minor tick marks is zero. .o see tick marks, you must e$plicitly set the spacing or either major or minor tick marks )or both* to a non7zero !alue and call the /et:aintTic;/(tr$e) method. 1o4e!er, you also need labels or your tick marks. .o display standard, numeric labels at major tick mark locations, set the major tick spacing, then call the set&aint0abels)true* method. .he e$ample program pro!ides labels or its slider in this 4ay. 'ut you are not constrained to using only these labels. 6ustomizing 0abels on a Slider sho4s you ho4 to customize slider labels. 8n addition, a ne4 slider eature a!ailable in J5K 6 allo4s you to set a ont or the JSlider component.
@ont 0ont = new @ont(CSeri0C* @ont.ITALIC* )8); 0ra%e/:erSecond./et@ont(0ont);

9hen you mo!e the slider:s knob, the state6hanged method o the slider:s 6hange0istener is called. ;or in ormation about change listeners, re er to 1o4 to 9rite a 6hange 0istener. 1ere is the change listener code that reacts to slider !alue changes:
&$blic #oid /tateC.an,ed(C.an,eE#ent e) > JSlider /o$rce = (JSlider)e.,etSo$rce(); i0 (D/o$rce.,etVal$eI/Ad9$/tin,()) > int 0&/ = (int)/o$rce.,etVal$e(); i0 (0&/ == ') > i0 (D0rozen) /to&Ani%ation(); ? el/e > dela1 = )''' / 0&/; ti%er./et2ela1(dela1); ti%er./etInitial2ela1(dela1 E )'); i0 (0rozen) /tartAni%ation(); ? ? ?

<otice that the state6hanged method changes the animation speed only i the get+alue8sAdjusting method returns alse. Many change e!ents are ired

as the user mo!es the slider knob. .his program is interested only in the inal result o the user:s action.

Customizing La els on a Slider


.he demo belo4 is a modi ied !ersion o the Slider5emo that uses a slider 4ith custom labels:

.he source 'or this %rogram can be 'ound in Slider2e%o-.9a#a. Click the Launch button to run SliderDemo( using Java Web Start (download JDK or later!. "lternativel#$ to com%ile and run the e&am%le #oursel'$ consult the e&am%le inde&.

.he 'ollowing code creates the slider and customi/es its labels0
//Create t.e /lider JSlider 0ra%e/:erSecond = new JSlider(JSlider.VERTICAL* @:SA6I"* @:SA6AB* @:SAI"IT); 0ra%e/:erSecond.addC.an,eLi/tener(t.i/); 0ra%e/:erSecond./et6a9orTic;S&acin,()'); 0ra%e/:erSecond./et:aintTic;/(tr$e); //Create t.e label table Ha/.table labelTable = new Ha/.table(); labelTable.&$t( new Inte,er( ' )* new JLabel(CSto&C) ); labelTable.&$t( new Inte,er( @:SA6AB/)' )* new JLabel(CSlowC) ); labelTable.&$t( new Inte,er( @:SA6AB )* new JLabel(C@a/tC) ); 0ra%e/:erSecond./etLabelTable( labelTable ); 0ra%e/:erSecond./et:aintLabel/(tr$e);

1ach ke#2value %air in the hashtable s%eci'ied with the /etLabelTable method gives the %osition and the value o' one label. .he hashtable ke# must be o' an Inte,er t#%e and must have a value within the slider3s range at which to %lace the label. .he hashtable value associated with each ke# must be a Co%&onent ob*ect. .his demo uses JLabel instances with te&t onl#. "n interesting modi'ication would be to use JLabel instances with icons or buttons that move the knob to the label3s %osition. )se the createStandardLabel/ method o' the JSlider class to create a set o' numeric labels %ositioned at a s%eci'ic interval. 4ou can also modi'# the table returned b# the createStandardLabel/ method in order to customi/e it.

The Slider !"#


.he 'ollowing tables list the commonl# used JSlider constructors and methods. See .he JCom%onent Class 'or tables o' commonl# used inherited methods. .he ",5 'or using sliders is divided into these categories0

Creating the Slider 6ine .uning the Slider3s "%%earance Watching the Slider 7%erate Working Directl# with the Data 8odel Creating the Slider Constructor "urpose Creates a hori/ontal slider with the range - to 1-and an initial value o' 9-. Creates a hori/ontal slider with the s%eci'ied minimum and ma&imum values. .he third int argument$ when %resent$ s%eci'ies the slider3s initial value. Creates a slider with the s%eci'ied orientation$ which must be either JSlider.H RI! "TAL or JSlider.VERTICAL. .he last three int arguments$ when %resent$ s%eci'# the slider3s minimum$ ma&imum$ and initial values$ res%ectivel#. Creates a hori/ontal slider with the s%eci'ied model$ which manages the slider3s minimum$ ma&imum$ and current values and their relationshi%s. "urpose Sets or gets the slider3s current value. .he set method also %ositions the slider3s knob. Sets or gets the orientation o' the slider. ,ossible values

JSlider(! JSlider(int min$ int ma&! JSlider(int min$ int ma&$ int value! JSlider(int orientation! JSlider(int orientation$ int min$ int ma&$ int value!

JSlider(:ounded;ange8odel!

6ine .uning the Slider3s "%%earance $ethod void set<alue(int! int get<alue(! void set7rientation(int!

int get7rientation(! void set5nverted(boolean! boolean get5nverted(! void set8inimum(int! int get8inimum(! void set8a&imum(int! int get8a&imum(! void set8a*or.ickS%acing(int! int get8a*or.ickS%acing(! void set8inor.ickS%acing(int! int get8inor.ickS%acing(!

are JSlider.H RI! "TAL or JSlider.VERTICAL. Sets or gets whether the ma&imum is shown at the le't o' a hori/ontal slider or at the bottom o' a vertical one$ thereb# inverting the slider3s range. Sets or gets the minimum or ma&imum values o' the slider. .ogether$ these methods set or get the slider3s range.

Sets or gets the range between ma*or and minor ticks. 4ou must call /et:aintTic;/(tr$e) 'or the tick marks to a%%ear.

void set,aint.icks(boolean! Sets or gets whether tick marks are %ainted on the slider. boolean get,aint.icks(! Sets or gets whether labels are %ainted on the slider. void set,aintLabels(boolean! 4ou can %rovide custom labels with /etLabelTable or boolean get,aintLabels(! get automatic labels b# setting the ma*or tick s%acing to a non2/ero value. void Sets or gets the labels 'or the slider. 4ou must call setLabel.able(Dictionar#! /et:aintLabel/(tr$e) 'or the labels to a%%ear. Dictionar# getLabel.able(! =ashtable createStandardLabels(int! =ashtable createStandardLabels(int$ int! set6ont(*ava.awt.6ont! $ethod void addChangeListener(ChangeListener! boolean get<alue5s"d*usting(! Creates a standard set o' numeric labels. .he 'irst int argument s%eci'ies the increment$ the second int argument s%eci'ies the starting %oint. When le't uns%eci'ied$ the starting %oint is set to the slider3s minimum number. Sets the 'ont 'or slider labels . Watching the Slider 7%erate "urpose ;egisters a change listener with the slider. Determines whether the user gesture to move the slider3s knob is com%lete. "urpose .he inter'ace re>uired 'or the slider3s data model. "n im%lementation o' the 5o$ndedRan,e6odel inter'ace. Sets or gets the data model used b# the slider. 4ou can also set the model b# using the constructor that takes a

Working Directl# with the Data 8odel Class% #nter&ace% or $ethod :ounded;ange8odel De'ault:ounded;ange8odel void set8odel(! get8odel(!

(in JSlider)

single argument o' t#%e 5o$ndedRan,e6odel.

'(amples that Use Sliders


.his table shows the e&am%les that use JSlider and where those e&am%les are described. )here *escri ed Slider2e%o .his section Slider2e%o- .his section Con#erter )sing 8odels$ =ow to )se ,anels '(ample +otes Shows a slider with labels at ma*or tick marks. Shows a vertical slider with custom labels. " measurement conversion a%%lication 'eaturing two sliders that share data and have custom 5o$ndedRan,e6odels.

Potrebbero piacerti anche