Writing A Large Application in The Real World Based On The Java™ Foundation Class/Swing (JFC/Swing) API
Writing A Large Application in The Real World Based On The Java™ Foundation Class/Swing (JFC/Swing) API
Almost anyone can write a small GUI application We will learn how to write a large GUI application that is both well built and easy to use
Session 1703
Learning Objectives
Improve your skills with large GUI applications by:
Understanding the key design elements Knowing where to start your program Knowing how to finish your program
Session 1703
Speakers Qualifications
Large GUI-based applications for 12 years Java AWT and JFC/Swing technology for 4 years OO Development for 10 years
Session 1703
Session 1703
Session 1703
Session 1703
Homogenous
Consistent behavior Consistent layout
Session 1703
Extensible
Easy to add more features Re-usable core components
Session 1703
Complete
Application appears finished All the implied features are there
Drag & drop Tool tips Toolbars Help
Installs easily
10
Session 1703
Session 1703
The Challenge
The JFC/Swing API alone is too low-level Hard to use the JFC/Swing API in a consistent way
Different programmers will create different looks
12
Session 1703
13
Session 1703
14
Session 1703
GUI Infrastructure
JFC/Swing
15 Session 1703
16
Session 1703
17
Session 1703
18
Session 1703
GUI Infrastructure
JFC/Swing
19 Session 1703
Dialogs Utilities
JFC/Swing
20 Session 1703
Dialogs Utilities
Actions
JFC/Swing
21
Session 1703
Session 1703
Action Action
Application Code
Action Action Action
Data Structures
GUI Infrastructure
JFC/Swing
23 Session 1703
What Is an Action?
Defines actionPerformed() Adds property Handling
addPropertyChangeListener() removePropertyChangeListener() putValue() getValue()
<<interface>>
ActionListener
<<interface>>
Action
AbstractAction
24
Session 1703
25
Session 1703
Button1Action
26
Session 1703
PropertyChangeListener notifies Button when something has changed ActionListener Notifies Action when button is pushed
Button1Action Button1Action
27
Session 1703
Examples of Actions
Raise a Dialog Undo/Redo Cut, Copy, Paste Save Show parts of the User Interface Etc.
28
Session 1703
Writing an Action
Defines actionPerformed() Adds 6 more methods Implements everything but actionPerformed() Implements actionPerformed() to raise a dialog
29 Session 1703
<<interface>>
ActionListener
<<interface>>
Action AbstractAction
MyDialogAction
Example
NAME
ACCELERATOR_KEY
SHORT_DESCRIPTION
30
Session 1703
Example
ActionListener()
PropertyChangeListener()
MyDialogAction
31 Session 1703
Adding Properties
Adding Properties
Adding Properties
Adding Properties
public MyDialogAction(JFrame f) { putValue(NAME, Show My Dialog); putValue(SHORT_DESCRIPTION, Show my special dialog); putValue(ACCELERATOR_KEY, KeyStroke.getKeyStroke( d, Event.ALT_MASK )); } public void actionPerformed(ActionEvent ev) { _dialog.setVisible(true); } }
40 Session 1703
Session 1703
44
Session 1703
45
Session 1703
46
Session 1703
47
Session 1703
PropDBSingleton Class
PropDB
getInstance() getProperty() load()
java.util.Properties
getProperty() load()
Singleton Class Queries a property with a key Add more properties from a file
48
Session 1703
Application Code
1. Load Properties 2. Build Actions
Action Action Action Action Action Action Action Action Action Action Action Action
49
Session 1703
New ClassGeneralAction
Subclass of AbstractAction Add Property Loading
50
Session 1703
GeneralAction
Defines actionPerformed() Adds 6 more methods Implements everything but actionPerformed() Adds property loading but does not implement actionPerformed()
51 Session 1703
<<interface>>
ActionListener
<<interface>>
Action AbstractAction
GeneralAction
String s= prop.getProperty(command + "." + SHORT_DESCRIPTION); if (s != null) putValue(Action.SHORT_DESCRIPTION, s); s= prop.getProperty(command + "." + Action.NAME); if (s != null) putValue(Action.NAME, s); // // -- now do mnemonic, accelerator, etc // } }
52 Session 1703
} }
53 Session 1703
} }
54 Session 1703
} }
55 Session 1703
String s= prop.getProperty(command + "." + SHORT_DESCRIPTION); if (s != null) putValue(SHORT_DESCRIPTION, s); s= prop.getProperty(command + "." + NAME); if (s != null) putValue( NAME, s);
String s= prop.getProperty(command + "." + SHORT_DESCRIPTION); if (s != null) putValue(SHORT_DESCRIPTION, s); s= prop.getProperty(command + "." + NAME); if (s != null) putValue( NAME, s); // // -- now do mnemonic, accelerator, etc
MyDialogAction
59
Session 1703
GeneralAction
MyDialogAction
60
Session 1703
61
Session 1703
62
Session 1703
Short!!! Same!!!
63
Session 1703
64
Session 1703
MyDialogAction
Property File
65 Session 1703
CopyAction
DeleteAction
EnableAllAction
66
Session 1703
Many Actions
Action Action Action Action Action Action Action Action Action Action
Application Code
Action Action Action
Lots of Actions!!!
GUI Infrastructure
JFC/Swing
67 Session 1703
68
Session 1703
LazyBuildAction
<<interface>>
LazyBuildAction
New abstract methods
build() activate()
public void actionPerformed(ActionEvent ev) { if (!_built) { Build the first time build(); _built= true; } activate(); }
70 Session 1703
LazyBuildAction
New abstract methods
build() activate()
public void actionPerformed(ActionEvent ev) { if (!_built) { build(); _built= true; } Activate every time activate(); }
71 Session 1703
Action AbstractAction GeneralAction MyDialogAction These guys are Finethey dont create a dialog
72
Session 1703
GUI Infrastructure
JFC/Swing
78 Session 1703
JFC/Swing
79 Session 1703
GeneralAction
LazyBuildAction
JFC/Swing
80 Session 1703
Session 1703
82
Session 1703
83
Session 1703
84
Session 1703
85
Session 1703
Start-up Time
Always use a splash screen Generally use a progress bar Do everything possible to speed it up
Be meticulous Constant vigilance
86
Session 1703
Session 1703
Installation Challenges
Easy Normal JRE (Java runtime environment)
88
Session 1703
Easy Installation
Very first impression of your application Almost Un-noticeable Quick Network installations are rarely easy!
89
Session 1703
Normal Installation
Just like any other application Feels normal for that platform No special request from the installer Nothing else to install
90
Session 1703
91
Session 1703
Summary
Homogeneous
Build a solid GUI infrastructure
Extensible
Build your application around Actions
Complete
Include the details to make it exceptional Include a clean and easy installation
92
Session 1703
Always Remember
A good program is like a sky-scraper... If you want to go high, first you better go deep
93
Session 1703
Session 1703
Session 1703