UNIT- S
Chapter -I
Tava - 8 Feature
O Lambda Expressions
Afunchonal Tntertaces
Detaut Mehods
ODate time APT
Chapter-a FX
JAVA EFFéetS GUn
Scene Builder
O Tava FX
window SHruature
Tava FX App
text a mage
@Displaying
@Event Handling
Notes in Scene Graph.
S Layeng out
Mowse EVents
TAVA -R
xy întroducHon to
released by ora ale on March - 18 th,90Iy
Java - 8 as concerirabed
Sun MicioSystems mainty
) înitially the Tavoa
on objects.
revisión Javá-& ocused on tunciona
3) After major
poqramming released atter Java-R
Changes nterfaces
H) Number ot new Yunchiora!
erpreMiony tö suppost Funciona
Lamb da
5) - The introduc d in Tava-&
are
introduced
Proqramning Static Methods
Defaults
Methods
Meibilty
in interfaces tor more
tava-&
in Tava-&
introduced
e me APR, Time.
the Dake E t H i c f e n t , S u p
of p
Date
o
&
r t
tor
chapter-) Expresions
to Lambda Nameles funCHon
Introducion
* but
Erpresion. s. nothing ritntroduced
atter
- kam bda uncHon "T is usé
& Anonymous Funchional
proqrarnming
&upport used iin
Tava - 8 to are 4irst
- Exprels ions
xpresiors
Lambda
he
'LISP" Proqramming kanquage
Python, Ruby
icj cttac#,
Ater that used in' programming adso
nous in Tava
and n0uS
E: Funchion
add(int a-, int b)
public void
&ystem out- printinCa
+b);
3
Lambda Expression is obtained Sreps.
Specifier
Step-4 Remove Access
Otisiais
'yoid add(int aj int b
System out: println Ca+b);
8kep- Remove Reurn type
gdd (igt a,int b)
printin(atb);"osisi te
Sy srem: gut
8tep-3 RemoveFunction,,name
4930+$9
Ciot oa, int b) Soig p o4
Systom. out- printo (a+b); ik
&ep- Lnsert Arrow( ) blw
(int ayiot, b) Bstern out: printnCatb);
Step-5 Remove data type
(a,b)'
Byarem ouk println (a+b);
#Loimbda Erpresion :
co,b) ysrerni gut- printin(a+b);
Functional înterface:
methad
(ntevface of only on abstract
conaists
- ohich Abstract Merbod)
called SAM(Single
-N is also Absttact Mehod
intertace conaistS
uncional
ot Single methods
- defaut & Static
number ot
proqram -1
Without trror:
Funchional interface
ínterface A
void addC);
defaudt void S u b e )
Statie void mulC)
3 implements A
3 int example
class tun
vötd add C)
public
main(string
state void
pubic
eXample obË = neu
funint
ebí addC;
obj· subC0;
Qutput
enecuted uoithoutnsadte :
Proqram -
ith error
funckioòal ntesface
interface A a
Void addC;
Void m ( ;
detault Void Sub)
Staic void dive
implerment
dass turn-int erample1
public vod add()
3
Public yoid m u c )
main(&tring argsri)
public SBatHe void f u n i n t e x a m p l e ( ) ;
eKarople abj = new
fun_int
cbj-addC);
obj'
obj" Sub();
3
eramplejava
c:\ Cac-A>avac tunint
funint_eNamplejava : | erTOY: unepected
OFuncionnterface annotahion Funchion al anterface
funchiool interfaco
A s not a
intec
multipte non- Overriding abstract methody Pound in
Defaudt Methods:
in Tava
interfaces could only have
- Befoe Tava 8,
Cmethods uo'thout a body
absthact methods metbods has to
be
implementation o these
The clas.
in a Separote
provided In
methods in interface,,
introduced
detaulE
Java & Cimplementat-on).
methods oih a body4lexíble. and backuuosd
allousing
interfaces m
mOre
-This makes and
defaulk
abstfàcf
compakble. both
now
bave
Anterfaces can
compatibiliy
methods sackuard
method
provide
pefault
- ex'sting a0de,ogt
deda akion
oithout breaking betore
method
is used
Keyoord
- Defaul
Syptax : methodname C)
rekurntype
defautt
Example :
interface A
int a=t0, b=0;
Void addC );
void subC);
tiplicakion +(a*b);
Sygtem-out printin(" Mul
3
ass enample implements A
publle void
d addC)
Ca+b).
[Link] println(" Addition : "
[Link] SubC )
System ouiprintnC"Subtraction +Ca-b)),
3
public, staHc void main(String argst 3)
enampleC;
bf- new example(; en
erample
Obj&o ddC;
cbj SubC ;
Obj mul();
3
Qutput
c:lauggaut63 >java eample ihe ib,
Addition 30
Subtracon : -t0
MultiplieaHon: 200
X Date Time APL :
APN:
introduced new data type
4) Tava- &
) f is available in Java time packaqe
import java . time *;
older ver sion
disadvantaqes ot the
ovescomes the
3) Dt util"date and
ot java ies java
java· util Calendan
Hrom util packaqe Date
& Time APn &
Some Disadvantaqes
c) thread unsate.
U) e s s Robustnes
dos Mutability etc...
I'6k ot doses g Mettods in time pockoqe
:) Local Date ony displays date
9 ) L,ocalTime - ’onty ditplaus ime
Date Tirhe A dake Eme
8) Local time oith Zone
u) ZonedDae Time ’ displys date,
ZonienDdisplays Zone entiiy
5)
Methods date it PrPresent
displays time
1)) nowc ) ’ bluo' ttme
data
Jor& -
a)
Program
LocalTime:
import java'Eime . w 4
DaFe Time;
(nport java Eime Local
public dass Date TimeExample
pa mainfString, argst3)
chapte public Shottc void DatenouwC);
date-L0al,
Local Date Date :" tdate);
println(Qusrent
System"out Timenouc);
time é Local
Loal Tirme
Sys-ern-outprinttn(:Crrent Time: '+time):
Time-nou);
'dateTing = Local
Date
LocalDakeTime Date and Time:
teshgdate Trme):
S y g t e m : o u t - p r i n t n ( Q u r r e n t
3
Oukput
C:\Tava>javac DateTimeExamplejavá'"
c:\Tava> java pateTime Example
Cutrent Date : 9025- l-o6
Cusrent Time: 18:5459. 54 P4BI00 st tuots
Current Dake and Tine 'o5-206 Tí8:54 :59.54tH3I400
Chapter -
Tava FX GhUT kit
iand GUT tool
) Java FK S a java Librany opplica ions , mobile
dekiop
a) ît is used tor developing etc.
opplicattons ete
applicationy, web
GUn
pplatform used
3) t is a Coss'
Apple ts , AwT & 8wngs
) upto Tava 4 e depricat ed.
after tha Appleks,
htertace
SiOT raphis Uger Sun Mic0
Micto ystny
on
Q004
QO04 by Sun
Oracle
5) Tava F* devel started Jaunched by
Dec,so8
t uÙas
but in or execuhon.
Seripting kanguage
used ngw Tava FK9
6) Tnitially Tava XML, fs
used, and
after yeson :5
FX dled tor
) Java desktop. web applicoons
Macos, wihdows inux
a / p l a t t o n hdependent
tdounload
&ource
jovax applteaion paCkage
applicaHon-Applicaton ;
-’ j import javatx
we need to Aeasn
’Main Cotaine
) Scene 3ub containe
>Sub
3) ’Arangermer,, otu controls
kooHovizonta
olumn Vehticaly.
u) Un contiols
-Button -TeztArea - RadioButtonor)
- Label -Choice
-TextFtld
- List
)EventS ’ an Action EVent
Buttonclick
X
&tage TextfieldEnter
Beene Lis Chotce Select
RadioButton liek,ts
Loyout Lnt er
bename
t
LAcHonsevent
Lobalb Possuod
Tet Field
Login | Cancel
Tavat appl'eation
windo SHructure
dick
Buttons
AChHon-tyent
Events.
LayoutUt
conttols
8tageScene
. Applicaton;
javafx. applieation
import
Stage-SBage;
import javatr . Scene;
import javatx gcene
* Lauouks
LsArangement ot Un
controls.
Hor(Rou)vente cal Cgol)
in
4) +HBor
-is used to arrage
1) controls Horitontolly.
letice ctice
tn aa &tage
-is used to arrangei
Un controls da ca
in a &taqe
)FlopPane
the &creen
ous of buttons
the
uot be arranged
Ho»izontally A Vokealy
Borde Pane
)(ett Diucue!
) Botom
) center
5) Guid Poane
12
cliek
dick
lick
Proqram
import [Link]
impott javahoapplication Applicatton;
inport javafr. StaqeStage;
tmport joyatyscen Control· Button;
Seene Scene
impott javafx
imþort javaf Scene layout Applicaion
public dass ENample etends
args])
publie statie void main(String
args
Launch):
Override
thidu0SEKCeplien
public void Start(Stage primaystage
xtio
bi= new ButtonC cAick Me)
Button
Scene(toot)
Scene SC= ne
setscene Csc);
primary Stage
Primary Stage - ghowc
);
3
Dulnas
Skage 'shoç) the .creen to the user
- S to
’se' the Seene conta'neonthe istage
Set Scene c) Stage 'iud
ser the title for a
3- SekTHec ttle tor a [Link]
the
F a' stage 'screen.
’Set the uid th
5 seWidth(value) ot a' Shage sren.
the heant
setHeight (value)’set
)’qet the uoidth. of,a sfage Screern
width
qet
a istage Sereen.
8 getHeightC) ’ get the height of
9: setfullScreen the stag e Sareen sets to entre
aystem sevcern
) Clse C)-cloç e the Stage &C reen
ouBomokicay
I) isful ScreenC) checks ohether Hhe givern Screen
’t gcseen & noL
se into full
oB a stagc s
* GsidPane :
Proqram
import javaf applicakion *;
impost java tx seeneScene;
mport javaB. Scenecontrol· Button j r ) s e
impor t javafx. Scene. layout Guid Panes
javafx scene
import javafx stage Stage ;
Public lass App ekends Applicaion
main(Strig'agst]) thouas Exkepla;
publtic Staic void
launch(args)»
Overside ELcepfon
primStoge) throuS
publc void &art (Stage
neu Button* click Me );
Button bi =
Button b= neÚ Button("click Me ;
Button(" cick Me);she
BuHon b3 = neuw
Buttonf" cdick Me;
Button b4 , neo
Button"click Me");
Button b5 = new
Grid Pane root Col= new Girid PaneC;
ro)
Soo add (bi,9,0);
sOot add (b9, 1, );
soot add(b3,a,a);"
Toot add (bu,33); p
oot; add (b5;0 5);o
Scene. sc = new Scenelroot);
RimnStage. setScene(sc);
primStage setwidth (uoo);
prinsage· sHeight (uo0);
primStage Shouw();
3
output:
Chckvel
@Overcide throwsExceptionf
start(Stage PrimSage)
.public void Button
Button("tek Me ) ;
Button' bl'=new
Biton 'b neuw Button ("Bultona");
Button (" ButtoD 3");**
Button b3 enew
Button (" ButtonH);:
new
Button by Button("Bü t o n 5 .
Button b5 ne
new Border PaneC;
BorderPane b=
b:SetLett(bi);
b set Rignt (oa);
BottonRight( b3);
b Set
b: se- pBttomlbu);
b-set Center(b5);
Soene sc= new Scene( b ) ; t a s y r o n
primßtagé setid tb( uoo);
primStage setHeiqht(uo0;
primStage ShowC); .
SetTtle(" THs My ApplicaFfon");
primgtage
Output
*Displasina îmage:
path to Tun --add.
pathc: iavat-Sdk- 85-0-1| ib
java -- module-
modules iavafx «controls,-cp bin App
to cöpy inagu, 6 image pnq and paate
* *irst use h¡ve
to display image
it in´sre.'and then' use' on able tO
in applic alfono*
Progran o rik in shgrt.
image'1 - ney, Image(get ClaysC) get Resourcets SH
Tmage
mageview imageView =new sew Smage'tu(tmage 4),'
loptionad : Resize the i a g e .
setFituidth(300);
imagevituw
imaqev'ew -set Pred erve Ratio(true);
Stack Pane root =neuD Stack Pare image view);
Scene gcene = new SCene(rook, uoo, uon).
primShage sct Scene (s cene );
ptimStage-setTite ( Diplay Image in javafx );
primStage Showc);
*Dieplaying Tet:
write in start
Poqram to
new Textthis is TayaFx Text");
Tet text =
tack Pane root ne Stack PaneCtext);
20o)
Scene(Toot, uoo,
Scene scene = new
Display Textin TavaFx);
primStage. seTHe(?
primStáge getScene (scene);
primßtaqeshow();
*
Mouye Events .
a
the tet Size on
increase
tont"Arial 30));
text. get Font(Font
- relpond to
Jayafx ollow you' to
&uchas clrckig,
Mouueeventsi6
with the mouse
uset interactions
compa nents
o, draqging on uf attached to
moving, pressiig:, eyent isteners
aré handled using
hades 'i2*loiespotda
:
Events iri TavaFx
Common, Mouse
button,is
Meuse Press moue
ccuts uchen a
MOUSE_PRESsED
"[Link]
button is
RELEASED; Oocurs usher
MoUSE
jele ased
cicked
uhen [Link] fs
ICKED: Occurs
MOUSE_ CLICKED:
(pressed and released)
MOUSE_MOVD: occurs when the mouseS noved
othout any button pressed
SMOUSt- DRAGGED: Occurg ohen the nmoule is moved
SMOUSE-DRAGGED:
oith a button pressed
when the
mouse Cursor
MoUSE-ENTERED: Occurs
node area.
enters a
EXmED:oceurs ushén. the mouse Cursor
cuSor
MOUSE
leavesa nod e area
How to .handle mouse ¬vents 39902
You add event
evenE handles
Mouseclickedt), seHOn Mouse PressedC), ekc.
8 setOn
Erample
Label abel =
= new Labell"Tnteract with me Wsing
[Link](Font: Pont(" Arial , 30)):
|/ ethandlstor al common mouse event.
Qabel- seonMoe Presed(e label SetTeyt(Mouse Preued)
label geonMöseReleased(e labelsetTent( "Mouse Releae
label- setôn Moúse Clicked(elibdi getText Mouse clicked'D;
label. getOnMoue Moved (e’ labe:Se Text(Moule Moved)
label· seton Mouse Dragged(e label geText(" Mouse Dragged)!
label-geton MouseEntered (elabel- 8et Text ( Mouse Enered)
lobe geton Mouse Eted (elabel- se Text(Mouye E ted');
StackParne Toot = new Stack Pane(-Label);
Scene Sç = new Seenelroot ,'uog 200):
primStaqe "ge TitHe(" Javafx Mouse Events Demo"):
primStoqe: et Scene (sc); iSgeni
primStage showl ); * .
Qutput
Sexeer-3
Mouye Psewcd |Mouse Relea Craw al
2Sereerj
tor
exam
Event Handles:
on an objec
but an actton
-Event is nothing events is calledd Even
ditterent types ot
To handle
Handler
Example Entered etc
Exited,
Moved, Typed ,
of availabie
dick, help
Can qenerate
event uol th the
&ources.
&ources licked Event.
, Button -generakes
Teleased
ekc.. .
Moved,
elicked,
2, Mouse - released
etc....
presed,
Typed, the &ource
3, key - an
event trom
nt +rom
Yeceives
listenes event Handler
- The
it to the
and sends