Reasoning about Data Abstractions - PowerPoint PPT Presentation

About This Presentation
Title:

Reasoning about Data Abstractions

Description:

Explain any changes to your project idea since the design review meeting ... Activating/Deactivating // in JButton: void setEnabled(boolean b) MODIFIES: this ... – PowerPoint PPT presentation

Number of Views:16
Avg rating:3.0/5.0
Slides: 22
Provided by: David77
Category:

less

Transcript and Presenter's Notes

Title: Reasoning about Data Abstractions


1
cs205 engineering software university of
virginia fall 2006
Wimpy Interfaces
2
Project Progress ReportsDue Monday, Nov 27
  • Updated description of your design
  • Explain any changes to your project idea since
    the design review meeting
  • Explain major changes to your design
  • Clear list of what you have done
  • Include print outs of code for some important
    modules
  • Explanation of your testing strategy (and how far
    you have gotten)
  • Plans for completing the project
  • List of what is left to do and how you will do it

3
public Component add(Component c)
Component
JComponent
Container
Window
JLabel
AbstractButton
Frame
JPanel
JButton
JFrame
...and hundreds (?) more subtypes in API
4
Adding Components
import javax.swing. import
java.awt. public class Main private
static void showGUI() //Create and set
up the window. JFrame frame new
JFrame("Swing GUI") java.awt.Container
content frame.getContentPane()
content.setLayout(new FlowLayout())
content.add(new JLabel ("Yo!"))
content.add(new JButton ("Click Me"))
frame.pack() frame.setVisible(true)

5
Making Buttons
  • public void addActionListener(ActionListener l)
    MODIFIES this
  • EFFECTS Adds an ActionListener l to the button.

java.awt.event interface ActionListener extends
EventListener void actionPerformed
(ActionEvent e) EFFECTS anything
Note this method is called when an action
occurs.
6
import javax.swing. import
java.awt. import java.awt.event. class
ButtonListener implements ActionListener
public void actionPerformed (ActionEvent e)
System.out.println ("Got a button press
e) public class Main private static
void showGUI() JFrame frame new
JFrame("Swing GUI") java.awt.Container
content frame.getContentPane()
content.setLayout(new FlowLayout())
content.add(new JLabel ("Yo!")) JButton
button new JButton ("Click Me")
button.addActionListener(new ButtonListener())
content.add(button) frame.pack()
frame.setVisible(true)
7
Action Events
Got a button press java.awt.event.ActionEventACT
ION_PERFORMED,cmdClick Me, when1163559916342,mod
ifiersButton1 on javax.swing.JButton,27,5,82x26
,alignmentX0.0,alignmentY0.5,
borderjavax.swing.plaf.BorderUIResourceCompoundB
orderUIResource_at_29ab3e,flags296,maximumSize,mini
mumSize, preferredSize,defaultIcon,disabledIcon
,disabledSelectedIcon, marginjavax.swing.plaf.I
nsetsUIResourcetop2,left14,bottom2,right14,p
aintBordertrue,paintFocustrue,
pressedIcon,rolloverEnabledtrue,rolloverIcon,ro
lloverSelectedIcon, selectedIcon,textClick
Me,defaultCapabletrue
8
class ButtonListener implements ActionListener
public void actionPerformed (ActionEvent e)
if (e.getActionCommand().equals ("On"))
System.out.println("On!") else if
(e.getActionCommand().equals("Off"))
System.out.println("Off!") else
System.out.println("Unrecognized button
press!") public class Main private
static void showGUI()
ButtonListener bl new ButtonListener()
JButton onButton new JButton ("On")
onButton.addActionListener(bl)
content.add(onButton) JButton offButton
new JButton ("Off") offButton.addAction
Listener(bl) content.add(offButton)

9
Activating/Deactivating
  • // in JButton
  • void setEnabled(boolean b)
  • MODIFIES this
  • EFFECTS If b, enables this.
  • Otherwise, disables this.

10
class ButtonListener implements ActionListener
public void actionPerformed (ActionEvent e)
if (e.getActionCommand().equals ("On"))
System.out.println("On!") else if
(e.getActionCommand().equals("Off"))
System.out.println("Off!") else
System.out.println("Unrecognized button
press!") public class Main private
static void showGUI()
ButtonListener bl new ButtonListener()
JButton onButton new JButton ("On")
onButton.addActionListener(bl)
content.add(onButton) JButton offButton
new JButton ("Off") offButton.addAction
Listener(bl) content.add(offButton)

Can we make clicking On enable the Off button
(and vice versa)?
11
Inner Classes
  • Added to JDK 1.1 (no JavaVM support)
  • Define a class inside a scope
  • It has access to variables in the containing
    scope
  • Including private instance variables!

12
public class Main private static void
showGUI() JFrame frame new
JFrame("Swing GUI") java.awt.Container
content frame.getContentPane()
content.setLayout(new FlowLayout())
final JButton onButton new JButton ("On")
final JButton offButton new JButton
("Off") class ButtonListener implements
ActionListener public void
actionPerformed (ActionEvent e)
if (e.getActionCommand().equals ("On"))
onButton.setEnabled(false)
offButton.setEnabled(true)
else if (e.getActionCommand().equals("Off"))
onButton.setEnabled(true)
offButton.setEnabled(false)
ButtonListener
bl new ButtonListener()
onButton.addActionListener(bl)
content.add(onButton)
13
Anonymous Classes
  • No need to give inner classes names!

var new Superclass ( ) // override
methods here
14
public class Main private static void
showGUI() JFrame frame new
JFrame("Swing GUI") java.awt.Container
content frame.getContentPane()
content.setLayout(new FlowLayout())
final JButton onButton new JButton ("On")
final JButton offButton new JButton
("Off") ActionListener bl new
ActionListener () public void
actionPerformed (ActionEvent e)
if (e.getActionCommand().equals ("On"))
onButton.setEnabled(false)
offButton.setEnabled(true)
else if (e.getActionCommand().equals("Off"))
onButton.setEnabled(true)
offButton.setEnabled(false)

onButton.addActionListener(bl)
content.add(onButton)
What is the actual type of bl?
15
Not just Buttons
Component
JComponent
AbstractButton
JToggleButton
JButton
JCheckBox
JRadioButton
http//java.sun.com/docs/books/tutorial/uiswing/co
mponents/button.html
16
Menus Too...
Awkward design JMenu is a button action is to
popup JPopupMenu JMenu contains a list of
JMenuItems Why is JMenu a subtype of JMenuItem?
JComponent
AbstractButton
JMenuBar
JPopupMenu
JButton
JMenuItem
JMenu
JCheckboxMenuItem
JRadioButtonMenuItem
http//java.sun.com/docs/books/tutorial/uiswing/co
mponents/menu.html
17
Concurrency in GUIs
  • Responsiveness of GUI depends on multiple threads
  • Swing thread types
  • Initial threads (start program)
  • One event dispatch thread (all event-handling
    code)
  • Worker threads (do time-consuming tasks in
    background)

Swing framework does most of the work
programmer doesnt need to create threads
18
Event Dispatch
  • Why is there only one event dispatch thread?

Hint did we need to synchronize?
One event thread means all compute-intensive work
should be done in worker threads. (Otherwise
interface freezes like Rhocasa).
19
Worker Threads
  • Create a background thread to do
    compute-intensive tasks

SwingWorker is added to Java 6 (but we have Java
5). Need to download it from http//java.sun.co
m/products/jfc/tsc/articles/threads/src/SwingWorke
r.java
20
invokeLater
import javax.swing. public class Main
private static void showGUI() ...
public static void main(String args)
javax.swing.SwingUtilities.invokeLater(new
Runnable() public void run()
showGUI() )
// in SwingUtilities public static void
invokeLater(Runnable doRun) EFFECTS Causes
doRun.run() to be executed asynchronously
on the AWT event dispatching thread. This will
happen after all pending AWT events have
been processed. This method should be
used when an application thread needs to
update the GUI.
21
Charge
  • Projects make progress!
  • lt 17 days until final reports are due
  • Fridays class network programming
Write a Comment
User Comments (0)
About PowerShow.com