Presentation is loading. Please wait.

Presentation is loading. Please wait.

GUI III IS 313 5.6.03.

Similar presentations


Presentation on theme: "GUI III IS 313 5.6.03."— Presentation transcript:

1 GUI III IS 313 5.6.03

2 Outline Quiz #2 Homework #2 questions? Event handling

3 Event Handling

4 Event handling

5 Menu item example JMenu fileMenu = new JMenu ("File");
JMenuItem exitItem = new JMenuItem ("Exit"); exitItem.addActionListener ( new ActionListener () { public void actionPerformed (ActionEvent e) System.exit(0); } );

6 Button example cancelButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) ListDialog.dialog.setVisible(false); } });

7 Getting a value back button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) String selectedName = ListDialog.showDialog(null, name.getText()); name.setText(selectedName); } });

8 MouseListener example
blankArea.addMouseListener(new MouseAdapter () { public void mouseClicked(MouseEvent e) MouseEventDemo2.this.saySomething( "Mouse clicked (# of clicks: " + e.getClickCount() + ")", e); } });

9 Anonymous class new ActionListener () {
public void actionPerformed (ActionEvent e) System.exit(0); }

10 Anonymous class diagram

11 Mouse example again blankArea.addMouseListener(new MouseAdapter () {
public void mouseClicked(MouseEvent e) MouseEventDemo2.this.saySomething( "Mouse clicked (# of clicks: " + e.getClickCount() + ")", e); } });

12 Access to External Values
Instance variables of enclosing class Final variables in creating method

13 Access to Values public class MyImagePanel { String m_baz;
public void foo () { method body ... } ... rest of class ... public void addImageLabel (String caption) { JLabel lbl = new JLabel (caption); final MyImage img = new MyImage (caption); lbl.addMouseListener ( new MouseAdapter () { public void mouseClicked (MouseEvent e) { // need to access the outer class bar(MyImagePanel.this); // need to call outer class method // access final variable // access instance variable MyImagePanel.foo (img, m_baz); }); ...

14 MouseListener interface
void mouseClicked(MouseEvent e) void mouseEntered(MouseEvent e) void mouseExited(MouseEvent e) void mousePressed(MouseEvent e) void mouseReleased(MouseEvent e)

15 Event handling strategies
Location of EH code external component class named inner class anonymous inner class Base class or interface used Implement XxxListener Extend XxxAdapter

16 Recommendation Use anonymous inner class Extend Adapter
unless EH is large, then used named inner class Extend Adapter unless all interface methods needed Note: no ActionAdapter class

17 Event handling thread Can only process one event at a time
Time-consuming event handlers tie up the interface no other actions registered

18 Example


Download ppt "GUI III IS 313 5.6.03."

Similar presentations


Ads by Google