Unformatted text preview:

Odds and Ends Java UI pt 1 COMP 401 Spring 2013 Lecture 20 3 28 2013 One Observer for an Observable with mulHple observable events Need a way to disHnguish calls to update in order to signal which event triggered the call lec20 v1 Observer Observable Recap Basic PaOern Observable provides method to register observers add remove Observers maintained in some sort of collecHon Update method of observers called when state of Observable changes Arguments to method provide reference to observable and possibly characterize the change that occurred MulHple observable events Observable provides separate methods for registering observers for each event Could also provide one registraHon method with addiHonal arguments If you want to support having the same observer for more than one event argument to update method needs to disHnguish which event occurred A5 CheckLineOfSight Midterm 2 Average 64 5 StdDev 13 0 Min 31 Max 85 Curve 180 160 140 120 100 Series1 80 60 40 20 0 0 20 R 31 54 3 3 0 7 Places the average at 2 75 B 40 60 80 100 A6 Java User Interface Programming AWT Original Java UI framework Normalized interface to naHve OS UI toolkits Advantages and Disadvantages Packages start with java awt Swing Pure Java More sophisHcated components More easily extended and customized Packages start with javax swing Class names generally start with J COMP 401 Spring 2012 7 Hello World lec20 v2 COMP 401 Spring 2012 8 Hello World JFrame main frame new JFrame main frame setTitle Hello World main frame setDefaultCloseOperaHon JFrame EXIT ON CLOSE JFrame Top level window setTitle text in window Htle setDefaultCloseOperaHon What happens when window is closed COMP 401 Spring 2012 9 Hello World JPanel main panel new JPanel main frame setContentPane main panel JPanel Generic container window Used to contain other user interface elements main frame setContentPane main panel Top level windows have a content pane Main area of window Other areas of window menu bar window Htle etc Replacing default content pane with our Jpanel Accept this as cookbook for now COMP 401 Spring 2012 10 Hello World main panel setLayout new BorderLayout Containers i e JPanel associated with a layout manager Determines how user interface elements are arranged within the container Di erent layout managers provide di erent types and styles of arrangement Some limit the number and locaHon of component elements Di er in exibility and sophisHcaHon BorderLayout Allows 5 components to be placed in one of 5 areas NORTH SOUTH EAST WEST CENTER Center area is given any extra space Areas not lled are collapsed COMP 401 Spring 2012 11 Hello World JLabel hello world label new JLabel Hello World hello world label setHorizontalAlignment SwingConstants CENTER hello world label setForeground Color BLUE hello world label setBackground Color YELLOW JLabel Simple text component Property seOers for alignment color etc Colors in Java represented by class java awt Color Prede ned colors available as class constants SwingConstants De nes a number of constants and enumeraHons used as symbolic names for several di erent methods COMP 401 Spring 2012 12 Hello World main panel add hello world label BorderLayout CENTER Every user interface component is contained by some parent Here we add the label to the main panel Second argument to add method depends on layout manager of container Di erent layout managers need support di erent kinds of arguments in order to specify where exactly newly added component will go COMP 401 Spring 2012 13 Hello World main frame setVisible true Top level window must be made visible UnHl now Java was waiHng unHl we had set everything up COMP 401 Spring 2012 14 Top Level Windows JDialog Dialog box top level windows Several types pre de ned for ease of use File choosers informaHonal input prompt etc JFrame Normal top level windows for UI JApplet We ll be ignoring this for now COMP 401 Spring 2012 15 JOpHonPane Examples JOpHonPane 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 execuHon is halted unHl dialog is dealt with Method returns response directly lec20 v3 COMP 401 Spring 2012 16 JFrame Non dialog top level window May or may not have a window bar LocaHon of which is operaHng system and look and feel speci c OperaHon de ned by what is placed in it General paOern 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 17 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 18 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 verHcally GridLayout Equal sized regular grid COMP 401 Spring 2012 19 Swing Components Text JLabel JTextField JTextArea BuOons JBuOon JCheckBox JRadioBuOon JToggleBuOon Sliders JSlider Lots of others COMP 401 Spring 2012 20 UI Events UI elements respond to interacHon by generaHng events Listeners are registered to receive noH caHon when a parHcular event occurs Di erent listener interfaces de ned for di erent kinds of events Listening method receives event informaHon as a parameter UI elements are like Observable Listeners are like Observer COMP 401 Spring 2012 21 BuOon Example lec20 v4 addAcHonListener AcHonListener l Method common to all types of buOons De ned in AbstractBuOon Parent of all buOon types AcHonListener interface void acHonPerformed AcHonEvent e AcHonEvent encapsulates all event informaHon AcHonEvent Parent class is AWTEvent Common informaHon for all event types getSource Returns reference to object that generated the event Other informaHon provided speci c to subclass COMP 401 Spring 2012 22


View Full Document

UNC-Chapel Hill COMP 401 - comp401sp13lecture20Java_UIpt1

Documents in this Course
Objects

Objects

36 pages

Recursion

Recursion

45 pages

Load more
Loading Unlocking...
Login

Join to view comp401sp13lecture20Java_UIpt1 and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view comp401sp13lecture20Java_UIpt1 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?