21
1 cs205: engineering software cs205: engineering software university of virginia fall 2006 Wimpy Interfaces

1 cs205: engineering software university of virginia fall 2006 Wimpy Interfaces

Embed Size (px)

Citation preview

Page 1: 1 cs205: engineering software university of virginia fall 2006 Wimpy Interfaces

1cs205: engineering software

cs205: engineering softwareuniversity of virginia fall 2006

Wimpy Interfaces

Page 2: 1 cs205: engineering software university of virginia fall 2006 Wimpy Interfaces

2cs205: engineering software

Project Progress ReportsDue Monday, Nov 27

1. Updated description of your design– Explain any changes to your project idea since

the design review meeting– Explain major changes to your design

2. 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)

3. Plans for completing the project– List of what is left to do and how you will do it

Page 3: 1 cs205: engineering software university of virginia fall 2006 Wimpy Interfaces

3cs205: engineering software

public Component add(Component c)

Component

Container

Window

Frame

JFrame

JComponent

JLabel AbstractButton

JButtonJPanel

...and hundreds (?) more subtypes in API

Page 4: 1 cs205: engineering software university of virginia fall 2006 Wimpy Interfaces

4cs205: engineering software

Adding Componentsimport 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); }

Page 5: 1 cs205: engineering software university of virginia fall 2006 Wimpy Interfaces

5cs205: engineering software

Making Buttons

public void addActionListener(ActionListener l) MODIFIES: thisEFFECTS: Adds an ActionListener l to the button.

java.awt.eventinterface ActionListener extends EventListener { void actionPerformed (ActionEvent e) EFFECTS: anything Note: this method is called when an action occurs.}

Page 6: 1 cs205: engineering software university of virginia fall 2006 Wimpy Interfaces

6cs205: engineering software

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); }

Page 7: 1 cs205: engineering software university of virginia fall 2006 Wimpy Interfaces

7cs205: engineering software

Action Events

Got a button press: java.awt.event.ActionEvent[ACTION_PERFORMED,cmd=Click Me, when=1163559916342,modifiers=Button1] on javax.swing.JButton[,27,5,82x26,alignmentX=0.0,alignmentY=0.5, border=javax.swing.plaf.BorderUIResource$CompoundBorderUIResource@29ab3e,flags=296,maximumSize=,minimumSize=, preferredSize=,defaultIcon=,disabledIcon=,disabledSelectedIcon=, margin=javax.swing.plaf.InsetsUIResource[top=2,left=14,bottom=2,right=14],paintBorder=true,paintFocus=true, pressedIcon=,rolloverEnabled=true,rolloverIcon=,rolloverSelectedIcon=, selectedIcon=,text=Click Me,defaultCapable=true]

Page 8: 1 cs205: engineering software university of virginia fall 2006 Wimpy Interfaces

8cs205: engineering software

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.addActionListener(bl); content.add(offButton); …

Page 9: 1 cs205: engineering software university of virginia fall 2006 Wimpy Interfaces

9cs205: engineering software

Activating/Deactivating

// in JButton:void setEnabled(boolean b)

MODIFIES: this EFFECTS: If b, enables this. Otherwise, disables this.

Page 10: 1 cs205: engineering software university of virginia fall 2006 Wimpy Interfaces

10cs205: engineering software

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.addActionListener(bl); content.add(offButton); …

Can we make clicking “On” enable the “Off” button (and vice versa)?

Page 11: 1 cs205: engineering software university of virginia fall 2006 Wimpy Interfaces

11cs205: engineering software

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!

Page 12: 1 cs205: engineering software university of virginia fall 2006 Wimpy Interfaces

12cs205: engineering software

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);

Page 13: 1 cs205: engineering software university of virginia fall 2006 Wimpy Interfaces

13cs205: engineering software

Anonymous Classes

• No need to give inner classes names!

var = new Superclass ( ) { // override methods here}

Page 14: 1 cs205: engineering software university of virginia fall 2006 Wimpy Interfaces

14cs205: engineering software

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?

Page 15: 1 cs205: engineering software university of virginia fall 2006 Wimpy Interfaces

15cs205: engineering software

Not just Buttons

http://java.sun.com/docs/books/tutorial/uiswing/components/button.html

Component

JComponent

AbstractButton

JButtonJToggleButton

JCheckBox JRadioButton

Page 16: 1 cs205: engineering software university of virginia fall 2006 Wimpy Interfaces

16cs205: engineering software

Menus Too...

http://java.sun.com/docs/books/tutorial/uiswing/components/menu.html

JComponent

AbstractButton

JButtonJMenuItem

JMenu JCheckboxMenuItemJRadioButtonMenuItem

JMenuBar JPopupMenu

Awkward design:JMenu is a button – action is to popup JPopupMenuJMenu contains a list of JMenuItem’sWhy is JMenu a subtype of JMenuItem?

Page 17: 1 cs205: engineering software university of virginia fall 2006 Wimpy Interfaces

17cs205: engineering software

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 doesn’t need to create threads

Page 18: 1 cs205: engineering software university of virginia fall 2006 Wimpy Interfaces

18cs205: engineering software

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).

Page 19: 1 cs205: engineering software university of virginia fall 2006 Wimpy Interfaces

19cs205: engineering software

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.com/products/jfc/tsc/articles/threads/src/SwingWorker.java

Page 20: 1 cs205: engineering software university of virginia fall 2006 Wimpy Interfaces

20cs205: engineering software

invokeLaterimport 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.

Page 21: 1 cs205: engineering software university of virginia fall 2006 Wimpy Interfaces

21cs205: engineering software

Charge

• Projects: make progress!< 17 days until final reports are due

• Friday’s class: network programming