EventHandling_Tutorial_Week_20_785ed06e-5916-495f-97a0-89b0dfcf3e83_158224_.pptx

Full Transcript

Event Handling in Java What is an Event? Change in the state of an object is known as event i.e. event describes the change in state of source. Events are generated as result of user interaction with the graphical user interface components. For example, clicking on a button, moving the mo...

Event Handling in Java What is an Event? Change in the state of an object is known as event i.e. event describes the change in state of source. Events are generated as result of user interaction with the graphical user interface components. For example, clicking on a button, moving the mouse, entering a character through keyboard, selecting an item from list, scrolling the page are the activities that causes an event to happen. What is Event Handling? Event Handling is the mechanism that controls the event and decides what should happen if an event occurs. For example, click on button, dragging mouse etc. The java.awt.event package provides many event classes and Listener interfaces for event handling. User Event Source Event generated interacts (buttons, Listeners with UI textbox) event related to the Perform action Listeners must register(add) with the event source to get notified when a event occurs Event handling Java Event classes and Listener interfaces User Source Event Registered Listener Listener Action Object Type Listener Interface Iterface Fired method Click a JButton ActionEven addActionListene ActionListener actionPerformed() button t r Press enter JTextField ActionEven addActionListene ActionListener actionPerformed() in the t r textfield Select a JComboBox ActionEven addActionListene ActionListener actionPerformed() new item t r ItemListener itemStateChanged( ItemEvent addItemListener ) Check or JCheckbox ActionEven addActionListene ActionListener actionPerformed() Uncheck t r ItemListener itemStateChanged( ItemEvent addItemListener ) Key Pressed JTextField, KeyEvent addKeyListener KeyListener keyPressed() Key Typed JTextArea keyTyped() Key keyReleased() Released Event Handling Event Handling can be implemented by: By implementing the listeners in the main class Using the outer class Using the anonymous class(inner class[only one time object can be created]) Event Handling Steps Step1: Create a class that implements the ListenerInterface (optional can be done in other ways also {prev. slide}) Step2: Add or register the listener to the source Step3: Implement the method to receive and process the event Steps to perform Event Handling Following steps are required to perform event handling: Register the component with the Listener Registration Methods For registering the component with the Listener, many classes provide the registration methods. For example: Button public void addActionListener (ActionListener a){} Method public void actionPerformed(ActionListener a){} MenuItem public void addActionListener (ActionListener a){} Method public void actionPerformed(ItemListener a){} TextField public void addActionListener(ActionListener a){} public void addTextListener(TextListener a){} Method public void textValueChanged(TextEvent e) TextArea public void addTextListener(TextListener a){} Method public void textValueChanged(TextEvent e) Checkbox public void addItemListener(ItemListener a){} Method public void itemStateChanged(ItemEvent e) Choice public void addItemListener(ItemListener a){} Method public void itemStateChanged(ItemEvent e) List public void addActionListener(ActionListener a){} public void addItemListener(ItemListener a){} Method public void itemStateChanged(ItemEvent e) Item ItemListener Interface Method public void itemStateChanged(ItemEvent e) Java event handling by implementing ActionListener 2) Java event handling by outer class 3) Java event handling by anonymous class 1) Java event handling by implementing ActionListener 2) Java event handling by outer class 3) Java event handling by anonymous class JOptionPane standard dialogs o Message dialog o Message text plus an OK button. o Confirm dialog o Yes, No, Cancel options. o Input dialog o Message text and an input field. Constructor Constructor Purpose JOptionPane(Object message) It is used to create an instance of JOptionPane to display a message. JOptionPane(Object message, It is used to create an instance of int messageType JOptionPane to display a message with specified message type and default options. JOptionPane Methods Methods What it does? showMessageDialog(Compon used to create an information- ent parentComponent, message Object message) showMessageDialog(Compon used to create a message dialog with ent parentComponent, given title and messageType. Object message, String title, int messageType) showConfirmDialog(Compon used to create a dialog with the ent parentComponent, options Yes, No and Cancel; with the Object message) title. showInputDialog(Component It is used to show a question- parentComponent, Object message dialog requesting input message) from the user parented to JOptionPane standard dialogs ERROR_MESSAGE INFORMATION_MESSAGE WARNING_MESSAGE QUESTION_MESSAGE JOptionPane standard dialogs

Use Quizgecko on...
Browser
Browser