CompSci 125 Lecture 19 GUI: Organizing classes, Mouse Events, KeyListener, KeyEventDispatcher Announcements GUI Review JFrame § Container § Title Bar includes the title text, and the minimize, maximize and close buttons § Content pane § Re-position the frame § Re-size the frame http://docs.oracle.com/javase/6/docs/api/javax/swing/JFrame.html Content Pane § Is the region in the JFrame where we can add/draw “stuff” § JFrame constructs the default pane as a JComponent § We can add other graphical components to it § Has a LayoutManager § getContentPane from the JFrame § setContentPane in the JFrame § add gui components to the content pane http://docs.oracle.com/javase/tutorial/uiswing/components/toplevel.html JPanel § Is a Container to which you may add other Components § May have a LayoutManager § Can be configured as double-buffered § You may add a JPanel to the content pane § Use setContentPane to replace the content pane with a JPanel § Implements a paintComponent(Graphics g) method § You may subclass a JPanel and override paintComponent § Then you can draw on the Graphics context JLabel § Displays text string, an icon or both § “Write only” --- no user input possible http://docs.oracle.com/javase/6/docs/api/javax/swing/JLabel.html JButton § Displays a button § Our class implements the ActionListener interface § Java calls ActionListener’s actionPerformed method when user clicks button http://docs.oracle.com/javase/6/docs/api/javax/swing/JButton.html ActionListener Interface § Our class includes “implements ActionListener” clause § Java ensures we implement the actionPerformed method § Java passes an ActionEvent parameter to actionPerformed § ActionEvent’s getSource method returns a reference to the object (e.g. an instance of a JButton or JTextField) in which the event occurred JTextField § Displays text (like a JLabel) § Unlike a JLabel, user can enter text § Our class implements ActionListener § Java calls actionPerformed when user modifies text § ActionEvent’s getSource method identifies source of event § JTextField’s getText returns reference to user’s text String § Many, many methods! http://docs.oracle.com/javase/6/docs/api/javax/swing/JTextField.html paintComponent and the Graphics Context § drawArc § drawImage § drawLine § drawOval § drawRect § drawString § fillArc § fillOval § fillRect § clearRect § getClipRect § setColor § setFont § more… JCheckBox § Labeled stateful check box § Text label describes an attribute § Check box displays associated state (selected or not) § Clicking box toggles its associated state § Useful methods § JCheckBox(“Label Text String”) § isSelected() § Our class implements either ActionListener or ItemListener interface JRadioButton § Labeled stateful button § Text label describes an attribute § Button displays associated state (selected or not) § Clicking button toggles its associated state § Only one button in group selected at any time § Useful methods § JRadioButton(“Selected Button’s Text String”,true) § JRadioButton(“Unselected Button Text”) § isSelected() § Define a ButtonGroup and add each JRadioButton to it § Our class typically implements ActionListener interface Layout Managers § BorderLayout: Five areas (north, south, east, west & center) § BoxLayout: Single row or column § CardLayout: Only one component visible at any one time. Consider using JTabbedPane instead. § FlowLayout: Arranges components left-to-right, adding rows as necessary § GridLayout: Grid of rows and columns § GridBagLayout: Like GridLayout, but components span cells § MigLayout: http://www.miglayout.com/QuickStart.pdf Associating a Layout Manager with the Content Pane § A JFrame has an associated Content Pane § JFrame constructs the default Content Pane as a JComponent § But getContentPane returns a reference to a Container § Three ways to Associate Layout Manager with the Content Pane 1. Replace the Layout Manager in the default content pane 2. Replace default content pane with a JPanel having a Layout Manager 3. Add a JPanel having a Layout Manager to the default content pane http://docs.oracle.com/javase/tutorial/uiswing/components/toplevel.html#contentpane 1. Replace the Layout Manager in the Default Content Pane //Recommended approach unless you need to build! //your own JPanel (to override paintComponent to! //gain drawing access to the Graphics context)! JFrame theFrame = new JFrame();! .! .! .! Container contentPane = theFrame.getContentPane();! contentPane.setLayout(new FlowLayout());! 2. Replace the Default Content Pane with your own JPanel //Recommended approach if you need your own JPanel! //to replace the default content pane! JFrame theFrame = new JFrame();! .! .! .! JPanel contentPane = new JPanel(new FlowLayout());! theFrame.setContentPane(contentPane);! 3. Overlay the Default Content Pane with your own JPanel //Your own JPanel covers the default content pane! //which continues to exist! JFrame theFrame = new JFrame();! .! .! .! JPanel contentPane = new JPanel(new FlowLayout());! theFrame.getContentPane().add(contentPane);! MouseListener MouseListener § Java can notify our program of mouse activity § Our class implements the MouseListener interface § mouseClicked § mouseReleased § mouseEntered § mouseExited KeyListener KeyListener § Described in text. BUT… § “To fire keyboard events, a component must have the keyboard focus” --- http:// docs.oracle.com/javase/ tutorial/uiswing/events/ keylistener.html § THUS… your ap will quit receiving KeyEvents when some control grabs the “focus” KeyEventDispatcher KeyEventDispatcher § Want to catch *all* keyboard events in the JFrame § Java’s KeyboardFocusManager controls processing of KeyEvents § KeyBoardFocusManager registers KeyEventDispatchers (in a chain) § KeyBoardFocusManager dispatches KeyEvents to the chain before sending them to the control having the current “focus” § Each KeyEventDispatcher in the chain has an opportunity to observe the KeyEvent § AND… the responsibility to not inadvertently break the dispatcher chain § http://docs.oracle.com/javase/7/docs/api/java/awt/KeyEventDispatcher.html KeyEventDispatcher: Register to Receive Key Events § Implement KeyEventDispatcher interface in your class § Find the KeyBoardFocusManager processor of all KeyEvents KeyboardFocusManager mgr= KeyboardFocusManager.getCurrentKeyboardFocusManager()! § Add instance of your class to Java’s chain of KeyEventDispatchers mgr.addKeyEventDispatcher(myKeyEventDispatcher)! § Java will begin sending KeyEvents to your KeyEventDispatcher § http://docs.oracle.com/javase/1.4.2/docs/api/java/awt/KeyboardFocusManager.html KeyEventDispatcher: dispatchKeyEvent method § Java passes a single KeyEvent parameter to dispatchKeyEvent § Use KeyEvent’s getID method to determine the event type § You likely care about KeyEvent.KEY_PRESSED events § Use KeyEvent’s getKeyCode to see the actual key code § Return false to ensure Java finishes keyboard processing § http://docs.oracle.com/javase/7/docs/api/java/awt/KeyEventDispatcher.html