Java UI pt 2 COMP 401 Spring 2013 Lecture 21 4 2 2013 Top Level Windows JDialog Dialog box top level windows Several types pre de ned for ease of use File choosers informaOonal input prompt etc JFrame Normal top level windows for UI JApplet We ll be ignoring this for now COMP 401 Spring 2012 2 JOpOonPane Examples JOpOonPane provide quick and easy dialog boxes showCon rmDialog Allows user to con rm a choice with a Yes No Cancel response showInputDialog Prompts for text input showMessageDialog Shows message waits for user to acknowledge All of these are modal Flow of execuOon is halted unOl dialog is dealt with Method returns response directly lec21 v2 COMP 401 Spring 2012 3 JFrame Non dialog top level window May or may not have a window bar LocaOon of which is operaOng system and look and feel speci c OperaOon de ned by what is placed in it General pa ern Main method of program sets up UI in a JFrame JFrame made visible Rest of program now responds asynchronously to user interface events Hello world revisited COMP 401 Spring 2012 4 Containment Hierarchy JPanel used as content pane for top level window is root of containment hierarchy All user interface elements must be placed in a container i e JPanel Containers can be nested within containers Groups UI elements together and allows sub layout COMP 401 Spring 2012 5 Layout Managers BorderLayout 5 areas NORTH EAST WEST SOUTH CENTER N E W S also known as PAGE START LINE START LINE END PAGE END BoxLayout Stacked either horizontally or verOcally GridLayout Equal sized regular grid Specify number of rows and columns at construcOon 0 indicates auto for that dimension Others Card Flow GridBag Goup Spring See h p docs oracle com javase tutorial uiswing layout visual html COMP 401 Spring 2012 6 Swing Components Text JLabel JTextField JTextArea Bu ons JBu on JCheckBox JRadioBu on JToggleBu on Sliders JSlider Lots of others COMP 401 Spring 2012 7 UI Events UI elements respond to interacOon by generaOng events Listeners are registered to receive noO caOon when a parOcular event occurs Di erent listener interfaces de ned for di erent kinds of events Listening method receives event informaOon as a parameter UI elements are like Observable Listeners are like Observer COMP 401 Spring 2012 8 Bu on Example lec21 v3 addAcOonListener AcOonListener l Method common to all types of bu ons De ned in AbstractBu on Parent of all bu on types AcOonListener interface void acOonPerformed AcOonEvent e AcOonEvent encapsulates all event informaOon AcOonEvent Parent class is AWTEvent Common informaOon for all event types getSource Returns reference to object that generated the event Other informaOon provided speci c to subclass COMP 401 Spring 2012 9 Java UI s so far Create a top level window Create a JPanel to be content pane Fill JPanel with other components Bu ons sliders etc Containers with components arranged in them And so forth Connect UI events with listeners Listeners take acOon as response AcOon may change update UI Make top level window visible COMP 401 Spring 2012 10 Swing Component Class Hierarchy java awt Component java awt Container javax swing JComponent JPanel JScrollPane JSplitPane JTabbedPane JToolBar JComboBox JLabel JList JProgressBar JSeparator JSlider JToolTip JTree COMP 401 Spring 2012 AbstractBu on JBu on JToggleBu on JCheckBox JRadioBu on JMenuItem JMenu 11 Listener Types Supported at awt Component ComponentListener FocusListener KeyListener MouseListener MouseMoOonListener MouseWheelListener Supported at awt Container Supported at AbstractBu on ContainerListener AcOonListener ChangeListener ItemListener Supported by individual components AcOonListener JComboBox ChangeListener JSlider JProgressBar COMP 401 Spring 2012 12 Keyboard Concepts Keyboard events go to component that currently has focus setFocusable boolean status isFocusable requestFocusInWindow Java provides a framework for managing focus in more sophisOcated ways if necessary h p docs oracle com javase tutorial uiswing misc focus html api KeyListener interface Typing of character vs pressing of key keyTyped KeyEvent e keyPressed KeyEvent e keyReleased KeyEvent e Basic sequence of events generated key press key typed key released Important KeyEvent methods char getKeyChar This is only valid for key typed events int getKeyCode isAltDown isControlDown isShilDown isMetaDown COMP 401 Spring 2012 13 Mouse Concepts MouseListener mouseClicked MouseEvent e mouseEntered MouseEvent e mouseExited MouseEvent e mousePressed MouseEvent e mouseReleased MouseEvent e MouseMoOonListener mouseDragged MouseEvent e mouseMoved MouseEvent e MouseEvent PosiOon info RelaOve to component getX getY getPoint Absolute posiOon getXOnScreen getYOnScreen Click info getClickCount getBu on Modi ers isAltDown isShilDown etc COMP 401 Spring 2012 14 Composing Widgets A set of related UI elements that act as a unit within your UI Basic idea Subclass JPanel In constructor create and arrange UI components Provide methods for a aching listeners requesOng current state of UI etc Could be straight delegaOon or could be mediated by more sophisOcated logic COMP 401 Spring 2012 15 Color Chooser Widget lec21 v4 Basic widget construcOon and set up lec21 v5 Wiring up internal behavior lec21 v6 Providing external behavior lec21 v7 DemonstraOng keyboard concepts Model View Pa ern Keep object represenOng the data separate from widget providing the UI Model Object represenOng logical enOty within your applicaOon View Object widget represenOng UI to model Inform when data changes Model View Inform when user interacts COMP 401 Spring 2012 17
View Full Document
Unlocking...