CMSC 341 Making Java GUIs Functional More on Swing Great Swing demo at http java sun com products plugin 1 3 1 01a demos jfc SwingSet2 SwingSet2Plugin html Just google for SwingSet Demo Java Now let s learn how to make GUIs functional 09 29 2007 CMSC 341 Events 2 Last Class Learned about GUI Programming Created two GUIs UppercaseConverter Calculator Now we will make them work 09 29 2007 CMSC 341 Events 3 Events Java uses an Event Delegation Model Every time a user interacts with a component on the GUI events are generated Events are component specific Events are objects that store information like the type of event that occurred the source of the event the time of an event to name a few 09 29 2007 CMSC 341 Events 4 Event Delegation Model Once the event is generated then the event is passed to other objects which handle or react to the event thus the term event delegation The objects which react to or handle the events are called event listeners 09 29 2007 CMSC 341 Events 5 Three Players Event source which generates the event object Event listener which receives the event object and handles it Event object that describes the event 09 29 2007 CMSC 341 Events 6 Revisiting our GUI We have already created a GUI How many components What are some possible events 09 29 2007 CMSC 341 Events 7 Example Click on UPPER JButton Generates an ActionEvent Event object is sent to an ActionListener that is registered with the UPPER JButton ActionListener handles in actionPerformed method 09 29 2007 Event public class Handler implements ActionListener public void actionPerformed ActionEvent e System out println Handling e CMSC 341 Events 8 Registering Listeners By having a class implement a listener interface it can contain code to handle an event However unless an instance of the class is registered with the component the code will never be executed Common novice error 09 29 2007 CMSC 341 Events 9 A Few More Java Events FocusEvent component gains or loses focus MouseEvent mouse is moved dragged pressed released or clicked WindowEvent window is iconified deiconified opened or closed TextEvent text is modified KeyEvent key is pressed depressed or both ContainerEvent components are added or removed from Container 09 29 2007 CMSC 341 Events 10 Corresponding Listeners FocusEvent FocusListener MouseEvent MouseListener MouseMotionListener WindowEvent WindowStateListener WindowListener WindowFocusListener TextEvent TextListener KeyEvent KeyListener ItemEvent ItemListener ContainerEvent ContainerListener 09 29 2007 CMSC 341 Events 11 Methods for Registering Listeners JButton addActionListener ActionListener a addChangeListener ChangeListener c addItemListener ItemListener i JList addListSelectionListener ListSelectionListener l 09 29 2007 CMSC 341 Events 12 UpperCaseConverter Example Goal When UPPER button is pressed the text in the textfield will be converted to upper case and appended into the text area When CLEAR button is pressed both the text field and the text area will be cleared Things to consider to accomplish goal What type of events do we need to respond to What listener interfaces do we need to implement 09 29 2007 CMSC 341 Events 13 Implementing an ActionListener Create as a separate class Create as an inner class No access to data in JFrame Access to JFrame data Must instantiate an object of this class to pass to addActionListener method Make the JFrame implement the interface Access to JFrame data No need to instanciate an object of this class have the this reference 09 29 2007 CMSC 341 Events 14 Implementing ActionListener import java awt event public class UpperCaseConverter extends JFrame implements ActionListener omitted code upper new JButton UPPER clear new JButton CLEAR Good to test for expected upper addActionListener this interaction as you go clear addActionListener this omitted code public void actionPerformed ActionEvent e Object obj e getSource if obj clear System out println Clear else if obj upper System out println Upper 09 29 2007 CMSC 341 Events 15 Implement Desired Behavior public void actionPerformed ActionEvent e Object obj e getSource JButtons JLabels if obj clear JTextFields and input setText JTextAreas all output setText have setText method to change their content else if obj upper String result input getText StringBuffer buffer new StringBuffer output getText buffer append result toUpperCase n output setText buffer toString 09 29 2007 CMSC 341 Events 16 Adding Functionality to the Calculator Need capability for telling the number to go to the left or right TextField Need to be able to perform operations If click and holding the ctrl button then number goes to the left else the right Use the operators themselves for the operations Need to be able to clear fields Convert the equal sign to a C for clear 09 29 2007 CMSC 341 Events 17 Slightly Modified GUI Notice the change Changed to C Changed all references from equals to clears in the code 09 29 2007 CMSC 341 Events 18 Add Listeners plus addActionListener this minus addActionListener this mult addActionListener this div addActionListener this clears addActionListener this dot addActionListener this for int i 0 i 10 i numbers i addActionListener this 09 29 2007 CMSC 341 Events 19 Implementing the actionPerformed Method First step is to implement the skeleton code that will recognize the different locations that are clicked Second step is to code for clicks with ctrl key pressed and not pressed Third step is to add desired behavior Helper methods would be helpful for the converting of text to floats and for the various arithmetic operations 09 29 2007 CMSC 341 Events 20 More ActionEvent Methods public void actionPerformed ActionEvent e String command e getActionCommand System out println command int modifiers e getModifiers if modifiers ActionEvent CTRL MASK System out println CTRL PRESSED 09 29 2007 CMSC 341 Events 21 Problem Unfortunately the code on the previous code can not differentiate between a button click with the control key down and a button click alone Next try MouseListener interface mousePressed mouseReleased mouseExited mouseClicked mouseEntered 09 29 2007 CMSC 341 Events 22 Changing to a MouseListener Change all ActionListener references to MouseListener references Remove actionPerformed method and add public void mouseClicked MouseEvent e int button e getButton Determines which System out println button button was pressed right or left String modifiers e getMouseModifiersText e getModifiers System out
View Full Document