Upload
hendi-kurniawanto
View
217
Download
0
Embed Size (px)
Citation preview
8/12/2019 Bab11-How to Use Sliders
1/9
http://docs.oracle.com/javase/tutorial/uiswing/components/slider.html
How to Use SlidersKomponen JSlider dapat diguankan user untuk memasukkan nilai numerisdengan 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)// VerticalJSlider aJSlider = new JSlider(JSlider.VERTICAL);// HorizontalJSlider 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 creen2e0a$lt5o$ndedRan,e6odel %odel = new 2e0a$lt5o$ndedRan,e6odel(3* '*
* 3 );JSlider aJSlider = new JSlider(%odel);aJSlider. et rientation(JSlider.VERTICAL);
http://docs.oracle.com/javase/tutorial/uiswing/components/slider.htmlhttp://docs.oracle.com/javase/tutorial/uiswing/components/slider.html8/12/2019 Bab11-How to Use Sliders
2/9
Menciptakan JSlider tanpa argument, maka akan terjadi horizontal sliderdengan de ault data model. Model mempunyai initial !alue "#, minimum #,ma$imum %##, dan e$tent #.
JSlider &ropertiesSetelah menciptakan JSlider, anda dapat memodi ikasi data dalam model.Anda dapat memperoleh model dengan method public 'ounded(angeModelgetModel)*, dan kemudian memodi ikasi secara langsung. Anda juga dapatmemanggil 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%$%
8/12/2019 Bab11-How to Use Sliders
3/9
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);
8/12/2019 Bab11-How to Use Sliders
4/9
// 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 hange0istener.
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 controlanimation 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.
http://www.oracle.com/technetwork/java/javase/javawebstart/index.htmlhttp://www.oracle.com/technetwork/java/javase/javawebstart/index.htmlhttp://www.oracle.com/technetwork/java/javase/downloads/index.htmlhttp://docs.oracle.com/javase/tutorial/uiswing/examples/components/index.html#SliderDemohttp://docs.oracle.com/javase/tutorial/uiswing/examples/components/index.html#SliderDemohttp://docs.oracle.com/javase/tutorialJWS/uiswing/components/ex6/SliderDemo.jnlphttp://www.oracle.com/technetwork/java/javase/javawebstart/index.htmlhttp://www.oracle.com/technetwork/java/javase/downloads/index.htmlhttp://docs.oracle.com/javase/tutorial/uiswing/examples/components/index.html#SliderDemo8/12/2019 Bab11-How to Use Sliders
5/9
'elo4 is the code rom the Slider2e%o.9a#a ile that creates the slider inthe pre!ious e$ample.
tatic 0inal int @:S 6I" = ';
tatic 0inal int @:S 6AB = 3';tatic 0inal int @:S I"IT = 8; //initial 0ra%e &er econd. . .JSlider 0ra%e :erSecond = new JSlider(JSlider.H RI! "TAL* @:S 6I"* @:S 6AB* @:S I"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 )orboth* 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, thencall 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 theselabels. ustomizing 0abels on a Slider sho4s you ho4 to customize sliderlabels. 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( Seri0 * @ont.ITALIC* 8);0ra%e :erSecond. et@ont(0ont);
9hen you mo!e the slider s knob, the state hanged method o the slider shange0istener is called. ;or in ormation about change listeners, re er to
1o4 to 9rite a hange 0istener . 1ere is the change listener code that reactsto 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 '); i0 (0rozen) tartAni%ation(); ? ??
8/12/2019 Bab11-How to Use Sliders
6/9
as the user mo!es the slider knob. his program is interested only in the inalresult 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 liderJSlider 0ra%e :erSecond = new JSlider(JSlider.VERTICAL* @:S 6I"* @:S 6AB* @:S I"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 tableHa table labelTable = new Ha table();labelTable.&$t( new Inte,er( ' )* new JLabel( Sto& ) );labelTable.&$t( new Inte,er( @:S 6AB/ ' )* new JLabel( Slow ) );labelTable.&$t( new Inte,er( @:S 6AB )* new JLabel( @a t ) );0ra%e :erSecond. etLabelTable( labelTable );
0ra%e :erSecond. et:aintLabel (tr$e);
http://docs.oracle.com/javase/tutorial/uiswing/examples/components/SliderDemo2Project/src/components/SliderDemo2.javahttp://docs.oracle.com/javase/tutorial/uiswing/examples/components/SliderDemo2Project/src/components/SliderDemo2.javahttp://www.oracle.com/technetwork/java/javase/javawebstart/index.htmlhttp://www.oracle.com/technetwork/java/javase/downloads/index.htmlhttp://docs.oracle.com/javase/tutorial/uiswing/examples/components/index.html#SliderDemo2http://docs.oracle.com/javase/tutorialJWS/uiswing/components/ex6/SliderDemo2.jnlphttp://docs.oracle.com/javase/tutorial/uiswing/examples/components/SliderDemo2Project/src/components/SliderDemo2.javahttp://www.oracle.com/technetwork/java/javase/javawebstart/index.htmlhttp://www.oracle.com/technetwork/java/javase/downloads/index.htmlhttp://docs.oracle.com/javase/tutorial/uiswing/examples/components/index.html#SliderDemo28/12/2019 Bab11-How to Use Sliders
7/9
ach ke#2value %air in the hashtable s%eci'ied with the etLabelTable method givesthe %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. hehashtable 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 useJLabel 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 tablereturned 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 "urposeJSlider(! Creates a hori/ontal slider with the range - to 1--and an initial value o' 9-.
JSlider(int min$ int ma&!JSlider(int min$ int ma&$ intvalue!
Creates a hori/ontal slider with the s%eci'iedminimum and ma&imum values. he third int argument$ when %resent$ s%eci'ies the slider3s initialvalue.
JSlider(int orientation!JSlider(int orientation$ int min$
int ma&$ int value!
Creates a slider with the s%eci'ied orientation$which must be either JSlider.H RI! "TAL orJSlider.VERTICAL . he last three int arguments$
when %resent$ s%eci'# the slider3s minimum$ma&imum$ and initial values$ res%ectivel#.
JSlider(:ounded;ange8odel!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.
6ine uning the Slider3s "%%earance
$ethod "urpose
void set
8/12/2019 Bab11-How to Use Sliders
8/9
int get7rientation(! are JSlider.H RI! "TAL or JSlider.VERTICAL .
void set5nverted(boolean! boolean get5nverted(!
Sets or gets whether the ma&imum is shown at the le'to' a hori/ontal slider or at the bottom o' a vertical one$
thereb# inverting the slider3s range.void set8inimum(int!int get8inimum(!void set8a&imum(int!int get8a&imum(!
Sets or gets the minimum or ma&imum values o' theslider. ogether$ these methods set or get the slider3srange.
voidset8a*or ickS%acing(int!int get8a*or ickS%acing(!voidset8inor ickS%acing(int!int get8inor ickS%acing(!
Sets or gets the range between ma*or and minor ticks.4ou must call et:aintTic (tr$e) 'or the tickmarks to a%%ear.
void set,aint icks(boolean! boolean get,aint icks(! Sets or gets whether tick marks are %ainted on the slider.
void set,aintLabels(boolean! boolean get,aintLabels(!
Sets or gets whether labels are %ainted on the slider.4ou can %rovide custom labels with etLabelTable orget automatic labels b# setting the ma*or tick s%acing toa non2/ero value.
voidsetLabel able(Dictionar#!Dictionar# getLabel able(!
Sets or gets the labels 'or the slider. 4ou must callet:aintLabel (tr$e) 'or the labels to a%%ear.
=ashtablecreateStandardLabels(int!=ashtablecreateStandardLabels(int$int!
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'tuns%eci'ied$ the starting %oint is set to the slider3sminimum number.
set6ont(*ava.awt.6ont! Sets the 'ont 'or slider labels .
Watching the Slider 7%erate
$ethod "urpose
void
addChangeListener(ChangeListener!;egisters a change listener with the slider.
boolean get
8/12/2019 Bab11-How to Use Sliders
9/9
(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 aredescribed.
'(ample )here*escri ed +otes
Slider2e%o his section Shows a slider with labels at ma*or tick marks.Slider2e%o- his section Shows a vertical slider with custom labels.Con#erter )sing 8odels $
=ow to )se
,anels
" measurement conversion a%%lication 'eaturingtwo sliders that share data and have custom
5o$ndedRan,e6odel s.
http://docs.oracle.com/javase/tutorial/uiswing/examples/components/index.html#SliderDemohttp://docs.oracle.com/javase/tutorial/uiswing/examples/components/index.html#SliderDemo2http://docs.oracle.com/javase/tutorial/uiswing/examples/components/index.html#Converterhttp://docs.oracle.com/javase/tutorial/uiswing/components/model.htmlhttp://docs.oracle.com/javase/tutorial/uiswing/components/model.htmlhttp://docs.oracle.com/javase/tutorial/uiswing/components/panel.htmlhttp://docs.oracle.com/javase/tutorial/uiswing/components/panel.htmlhttp://docs.oracle.com/javase/tutorial/uiswing/examples/components/index.html#SliderDemohttp://docs.oracle.com/javase/tutorial/uiswing/examples/components/index.html#SliderDemo2http://docs.oracle.com/javase/tutorial/uiswing/examples/components/index.html#Converterhttp://docs.oracle.com/javase/tutorial/uiswing/components/model.htmlhttp://docs.oracle.com/javase/tutorial/uiswing/components/panel.htmlhttp://docs.oracle.com/javase/tutorial/uiswing/components/panel.html