Docsity

GUI Programming: Synchronous vs Asynchronous Input and Event Handling in Java, Study Guides, Projects, Research of Computer Science

Toggle sidebar
bg1
Intro to Programming II
GUI programming
Chris Brooks
Department of Computer Science
University of San Francisco
Departmentof Computer Science —University of San Francisco – p.1/??
16-2: Synchronous vs Asynchronous input
•The programs you’ve built so far (lexer and parser) are
examples of synchronous input.
◦You prompt for input, then read input with a Scanner.
•Programs with a graphical user interface (GUI) typically require
asynchronous input
◦A user can provide input at any time.
•This requires a different model of programming.
Departmentof Computer Science —University of San Francisco – p.2/??
16-3: GUI parts
•A GUI consists of:
◦Components
◦Events
◦Listeners
Departmentof Computer Science —University of San Francisco – p.3/??
16-4: GUI parts
•Components generate events (usually in response to user
input)
•Listeners wait for and handle these events
•Typically by invoking a method.
Departmentof Computer Science —University of San Francisco – p.4/??
16-5: Example: pt 1
•Open eclipse and create a new project called ’Class-project’
•Choose New-Other-GUI Forms-Swing-JFrame
•Give the subclass the name ExampleJFrame
◦A JFrame is an example of a top-level container
◦Other components are added inside the JFrame
Departmentof Computer Science —University of San Francisco – p.5/??
16-6: Adding components
•Choose ’Absolute Layout’ and then add three buttons and a text
field.
•Give each button a different label.
•Look at the code that Jigloo generates.
•Use the color wheel to change each button’s background color.
Departmentof Computer Science —University of San Francisco – p.6/??
pf3
pf4

Partial preview of the text

Download GUI Programming: Synchronous vs Asynchronous Input and Event Handling in Java and more Study Guides, Projects, Research Computer Science in PDF only on Docsity!

Intro to Programming II

GUI programming

Chris Brooks Department of Computer Science University of San Francisco ?? Department of Computer Science — University of San Francisco – p.1/ Synchronous vs Asynchronous input 16-2: The programs you’ve built so far (lexer and parser) are • input. synchronous examples of You prompt for input, then read input with a Scanner. ◦ Programs with a graphical user interface (GUI) typically require • input asynchronous A user can provide input at any time. ◦ This requires a different model of programming. • ?? Department of Computer Science — University of San Francisco – p.2/ GUI parts 16-3: A GUI consists of: • Components ◦ Events ◦ Listeners ◦ ?? Department of Computer Science — University of San Francisco – p.3/ GUI parts 16-4: Components generate events (usually in response to user • input) Listeners wait for and handle these events • Typically by invoking a method. • ?? Department of Computer Science — University of San Francisco – p.4/ Example: pt 1 16-5: Open eclipse and create a new project called ’Class-project’ • Choose New-Other-GUI Forms-Swing-JFrame • Give the subclass the name ExampleJFrame • top-level container A JFrame is an example of a ◦ Other components are added inside the JFrame ◦ Adding components 16-6: Choose ’Absolute Layout’ and then add three buttons and a text • field. Give each button a different label. • Look at the code that Jigloo generates. • Use the color wheel to change each button’s background color. •

Handling events 16-7: When a user provides input to a component, an event is • generated. For example, when the mouse is pressed or released. ◦ Select button1, then choose ’Mouse Listener-mouse released’ • under the ’Events’ tag. Select ’handler method’ • Look at the code Jigloo generates. • ?? Department of Computer Science — University of San Francisco – p.7/ Handling events 16-8: Now, we need to fix the event handler to do something • interesting. Let’s place the button’s label in the text field. • { evt) eas ed (Mo us eE ven t button1MouseRel void private evt); + event=" n( "b utt on 1.m ou seR el ea sed , System.out.printl tt on 1.g et Lab el ()) ; output.setText(bu } Add similar event handlers for button2 and button3. • ?? Department of Computer Science — University of San Francisco – p.8/ Adding more components 16-9: Now, let’s add a JList. • Jlists use a DefaultComboBoxModel to control access to their • data. Let’s add an Event handler to change the Jlist’s contents if the • return key is pressed. Choose Select the textfield, then choose KeyListener-KeyTyped • under ’Events’. Take a look at the generated code. • ?? Department of Computer Science — University of San Francisco – p.9/ Handling keyboard events 16-10: We need to look at the event and find out what key was • pressed. { evt) Key Ev ent outputKeyTyped( void private evt); + event=" n( "o utp ut .ke yT ype d, System.out.printl ’ == ) (evt.getKeyChar( if { n’) xMo de l)t hi sLi st .ge tM od el( ); (DefaultComboBo = m ode l DefaultComboBoxM put .g et Tex t( )); m.addElement(out } } The DefaultComboBoxModel controls access to the list • contents. ?? Department of Computer Science — University of San Francisco – p.10/ Model-View-Controller 16-11: What’s this model stuff about? • A common technique for GUI design (and OO design more • model-view-controller generally) is called A GUI should be separated into pieces: • the model controls the data itself ◦ The view controls how the data is displayed. ◦ The controller govers how the data is accessed and ◦ changed. Layout Managers 16-12: The Absolute manager is nice, but limited. • Try resizing your app. ◦ If we want to resize, we must pick a different layout manager. •

Exercise: building a simple calculator 16-19: Add a method to compute: • { compute() double private operand2); + " op2: " + operand1 + " n( "o p1: System.out.printl { ’+’) == (operator if operand2; + operand1 return { ’-’) == (operator if else } operand2; - operand1 return { ’*’) == (operator if else } operand2; * operand1 return { else } operand2; / operand1 return } } ?? Department of Computer Science — University of San Francisco – p.19/