Slide #1.

Events in Java Swing Chris North cs3724: HCI
More slides like this


Slide #2.

Typical command line program • Non-interactive • Linear execution program: main() { code; code; code; code; code; code; code; code; code; code; code; code; }
More slides like this


Slide #3.

Interactive command line program • User input commands • Non-linear execution • Unpredictable order • Much idle time program: main() { decl data storage; initialization code; loop { get command; switch(command) { command1: code; command2: code; … } } }
More slides like this


Slide #4.

Typical GUI program • User input commands • Non-linear execution • Unpredictable order • Much idle time GUI program: main() { decl data storage; initialization code; create GUI; register callbacks; main event loop; } • Event callback procs Callback1() //button1 press { code; } Callback2() { code; }
More slides like this


Slide #5.

GUI Events App1 mouse click OK App2 OK Cancel Cancel App2 code: App1 event loop Window System input device event loop which app? App2 event loop which callback? OKbtn_click() { do stuff; } OKbtn_mouseover() { do more stuff; } CancelBtn_click() { do different stuff; }
More slides like this


Slide #6.

Java Swing program Java program: • Event loop automatic in separate thread Class{ main() { decl data storage; initialization code; create GUI objects; register listeners; } listener1() { do stuff; } listener2() { do stuff; } …
More slides like this


Slide #7.

Example Example: draw program MyDrawClass{ main() { DataStruct drawn_shapes; drawn_shapes.clear(); create Frame, Panel, buttons, … register listeners; } DrawPanel_listener_click() { drawn_shapes.add(new shape); } UndoButton_listener_click() { drawn_shapes.deleteLast(); } …
More slides like this


Slide #8.

Java Listeners 1. Register with a component to receive events • • Give component a ref to your Listener object JButton1.addMouseListener(new myMouseListener) 2. Receive events from component • • Component will call callback procs on your Listener object myMouseListener.mouseClicked(event) click JButton1 1. addMouseListener( ) 2. mouseClicked( ) myMouseListener
More slides like this


Slide #9.

Listener API • Listeners must inherit from Java Listener base classes • ActionListener, KeyListener, MouseListener, MouseMotionListener, WindowListener, … • Abstract base classes: xxxxListener • Stubbed base classes: xxxxAdapter • MouseListener: • mouseClicked(), mouseEntered(), mouseExited(), mousePressed(), mouseReleased()
More slides like this


Slide #10.

Code button1 = new JButton(“press me”); myListener = new myListenClass; button1.addMouseListener(myListener); // extending a class (“subclassing”): class myListenClass extends MouseAdapter { public void mouseClicked(MouseEvent e){ // button clicked, do stuff here } } // OR “implementing an interface”: class myListenClass implements MouseListener { public void mouseClicked(MouseEvent e){ // button clicked, do stuff here } … } An abstract base class (methods, no code)
More slides like this


Slide #11.

Event objects • mouseClicked(MouseEvent e) • MouseEvent: • getX( ), getY( ), getClickCount( ), getSource( ), … • For each listener type: • • • • Component.addxxxxListener( ) xxxxListener abstract base class xxxxAdapter stubbed base class xxxxEvent
More slides like this


Slide #12.

Inheritance with Swing class myPanel extends JPanel { public myPanel(){ //constructor // create buttons, … } public void paintComponent(Graphics g){ super.paint(g); //call overriden method // paint stuff here } } • myPanel creates JPanel via inheritance • Override JPanel methods to add functionality
More slides like this


Slide #13.

Simplifying: Implements abstract class myPanel extends JPanel base class implements MouseListener { public myPanel(){ //constructor button1 = new JButton(“press me”); button1.addMouseListener(this); add(button1); } public void mouseClicked(MouseEvent e){ // button clicked, do stuff here } … }
More slides like this


Slide #14.

Simplifying: Anonymous classes class myPanel extends JPanel { public myPanel(){ button1 = new JButton(“press me”); button1.addMouseListener( new MouseAdapter() { public void mouseClicked(MouseEvent e){ // button clicked, do stuff here } } ); Defining and add(button1); instantiating a } class on the fly }
More slides like this


Slide #15.

In JBuilder • • • • Application JFrame, JPanel, JButton Layout managers Event listeners
More slides like this


Slide #16.

Homework #3 • Painter Applet
More slides like this