GUI IIIOutlineEvent HandlingEvent handlingMenu item exampleButton exampleGetting a value backMouseListener exampleAnonymous classAnonymous class diagramMouse example againAccess to External ValuesAccess to ValuesMouseListener interfaceEvent handling strategiesRecommendationEvent handling threadExampleGUI IIIIS 3135.6.03OutlineQuiz #2Homework #2 questions?Event handlingEvent HandlingEvent handlingMenu item exampleJMenu fileMenu = new JMenu ("File");JMenuItem exitItem = new JMenuItem ("Exit");exitItem.addActionListener ( new ActionListener () { public void actionPerformed (ActionEvent e) { System.exit(0); } });Button examplecancelButton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { ListDialog.dialog.setVisible(false); } });Getting a value backbutton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { String selectedName = ListDialog.showDialog(null, name.getText()); name.setText(selectedName); } });MouseListener exampleblankArea.addMouseListener(new MouseAdapter () { public void mouseClicked(MouseEvent e) { MouseEventDemo2.this.saySomething( "Mouse clicked (# of clicks: " + e.getClickCount() + ")", e); } });Anonymous classnew ActionListener (){ public void actionPerformed (ActionEvent e) { System.exit(0); }}Anonymous class diagramF o o $ 1A c t i o n L i s t e n e r« i m p l e m e n t s »Mouse example againblankArea.addMouseListener(new MouseAdapter () { public void mouseClicked(MouseEvent e) { MouseEventDemo2.this.saySomething( "Mouse clicked (# of clicks: " + e.getClickCount() + ")", e); } });Access to External ValuesInstance variables of enclosing classFinal variables in creating methodAccess to Valuespublic 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); } }); ...MouseListener interfacevoid mouseClicked(MouseEvent e) void mouseEntered(MouseEvent e) void mouseExited(MouseEvent e) void mousePressed(MouseEvent e) void mouseReleased(MouseEvent e)Event handling strategiesLocation of EH codeexternalcomponent classnamed inner classanonymous inner classBase class or interface usedImplement XxxListenerExtend XxxAdapterRecommendationUse anonymous inner classunless EH is large, then used named inner classExtend Adapterunless all interface methods neededNote: no ActionAdapter classEvent handling threadCan only process one event at a timeTime-consuming event handlerstie up the interfaceno other actions
View Full Document