Вы находитесь на странице: 1из 32
PI2133 Pemrograman Berorientasi Objek OOP dengan Java -Event Handler GUI
PI2133
Pemrograman Berorientasi
Objek
OOP dengan Java
-Event Handler GUI
Previous  We already build a GUI for application (using IDE ex. Net Beans) 
Previous
 We already build a
GUI for application
(using IDE ex. Net
Beans)
 Next  Control
interaction between
GUI and USER (make
it work)
Basic Idea –Delegation Model  Event – Objects that describe what happened  Event source
Basic Idea –Delegation Model
 Event – Objects that describe what
happened
 Event source – The generator of an
event’
For example, a mouse click on a Button
component generates an ActionEvent
with the button as the source
 Event handlers – A Method that receives
an event object, deciphers it, and
processes the user’s interaction
Delegation Model 1. User click this button 2. An Object that type is ActionEvent automatically
Delegation Model
1. User click
this button
2. An Object that type is
ActionEvent
automatically generated
3. The Code in event
Handler that assosiate
whit the event Execute
How That Things Can be Done? [1]  User click a Button  it means
How That Things Can be Done?
[1]
 User click a Button
 it means the button has capabilities to
generate event (became event-source)
 There were an interface that commonly
named “Listener” in this case we use
ActionListener
 It means that we must have a class that
implements “Listener” so we can create a
Object to Listen what’s going on?
How That Things Can be Done?[2]  User click a Button (cont’d)  Simple way
How That Things Can be
Done?[2]
 User click a Button (cont’d)
 Simple way (manually)
 Make the frame implements ActionListener,
consequently we must “realize” method
from ActionListener Interface
How That Things Can be Done?[3]  User click a Button (cont’d)  Now our
How That Things Can be
Done?[3]
 User click a Button (cont’d)
 Now our class has capabilities to
“Listening” what’s going on and what
had to do if something happen?
 Is it finish?? => Who is the target to
“Listening” , in this example is
 Now we must add the
object to be “Listening”
to the list of
How That Things Can be Done?[4]  User click a Button (cont’d)  With this
How That Things Can be
Done?[4]
 User click a Button (cont’d)
 With this code, if some user click the
ButtonAdd the event can be detected by
the “Listener” [process 1 and 2 finish]
How That Things Can be Done?[5]  Make Event Handler  From the previous code
How That Things Can be
Done?[5]
 Make Event Handler
 From the previous code we know that
there are two posible event source
[ButtonAdd,ButtonMul] and the handle
code is different
 We must know which button is click by
the USER (the event is same ActionEvent
but the EventSource is different)
How That Things Can be Done?[6]  Make Event Handler
How That Things Can be
Done?[6]
 Make Event Handler
Result
Result
More Technique  Please pay Attention  Simple way (manually)  So is there another
More Technique
 Please pay Attention
 Simple way (manually)
 So is there another sophisticated
way? Do we need It?
 Another way is to separated the
event-handler code from the GUI
code => achieve more “Reusability”
Huaaaa…. Sueetress bo,… Semangat biar ntar lulus dapet kerja Ampun Pak… Kok banyak banget caranya??
Huaaaa…. Sueetress
bo,…
Semangat biar ntar
lulus dapet kerja
Ampun Pak… Kok
banyak banget
caranya??
Baca buku lagi
dech
Duh mumet
euy
Another Ways  This knowledge can help U to understand the basic of MVC model,
Another Ways
 This knowledge can help U to
understand the basic of MVC model,
so keep your spirit on fire!!!
 The main idea is to split the code for
event-handler with the GUI-code,
Another Ways  Step-by-step  Create another class to implements the “Listener interface”  Make
Another Ways
 Step-by-step
 Create another class to implements the
“Listener interface”
 Make method for handler and
adjustment the code specially on access
specifier issue
 Create Object of handler
 Register the object to be “snipe” to
object handler
Another Ways [step1, step2]
Another Ways [step1, step2]
Another Ways [step2]  Adjustment on class for GUI
Another Ways [step2]
 Adjustment on class for GUI
Another Ways [step 3, step 4]  We now modified the code in GUI class
Another Ways [step 3, step 4]
 We now modified the code in GUI
class for:
 instantiate object of handler
 Connect the handler and gui
 Register object that involve in generate
the event
Another Ways [step 3, step 4] The GUI class doesn’t has to implements the Listener
Another Ways [step 3, step 4]
The GUI class doesn’t has to implements the Listener interface,
because there is another class who has that responsibility
Another Ways  Run the code and you will get the same result with the
Another Ways
 Run the code and you will get the
same result with the first technique
 So what the benefit we can get, it
more complicated ??
Benefit  Reusability  When we separate the handler code from GUI code, we can
Benefit
 Reusability
 When we separate the handler code from
GUI code, we can use it for different GUI
it make our code more suitable and easy
 To show it, let’s make another GUI
with different Look/Component but
have same purpose
Different Look GUI same purpose Tipe2 This GUI had different look with the first GUI,
Different Look GUI same
purpose Tipe2
This GUI had different look with the
first GUI, and the Component to
view The Result is different with the
first GUI
The Code [1]  Our target is to  use the handler code without adjustment
The Code [1]
 Our target is to
 use the handler code without adjustment
 The adjustment only in the GUI Code
 There is a problem in the previous
definition of handler class the type for
guinya is Tipe1,we must change it to
Tipe2 in order to make it work!!
The Code [2]  Solution Create an Interface and Tipe1 and Tipe2 should implements this
The Code [2]
 Solution Create an Interface and
Tipe1 and Tipe2 should implements
this interface
TipeUTAMA
Tipe1
Tipe2
 Change the tipe of guinya in event
handles class to TipeUtama
The Code [3]
The Code [3]
The Code [4]  Now guinya tipe is TipeUtama, but there is a problem =>
The Code [4]
 Now guinya tipe is TipeUtama, but
there is a problem => when we use
reference tipe interface we can only
call method that has been define in
the interface
The Code [4]  Solution  Add prototype of the method that we will use
The Code [4]
 Solution
 Add prototype of the method that we will
use in the event handler calss (related to
GUI class)
The Code [4]  Make adjustment code in class Tipe 2
The Code [4]
 Make adjustment code in class Tipe 2
The Code [5]  Set the main project to Tipe2 and woala the program run
The Code [5]
 Set the main project to Tipe2 and
woala the program run and have
same function with the first one
Result
Result
Next->Konsep MVC CONTROL MODEL VIEW [Event- [Data [GUI] Handler] /Proses]
Next->Konsep MVC
CONTROL
MODEL
VIEW
[Event-
[Data
[GUI]
Handler]
/Proses]
Any Question ??
Any Question ??