Graphical User Interfaces in Java Part 2

slimwhimperSoftware and s/w Development

Nov 3, 2013 (3 years and 9 months ago)

50 views

1
1
Graphical User Interfaces in Java
Part 2
COMP440
Penn State Harrisburg
April 17, 2007
© Julia M. Lobur
2
Objectives
• Illustrate Swing look-and-feel.
• Demonstrate the relationship between
concurrency and Java Swing.
• Present Java Beans.
• Discuss alternatives to Swing.
2
3
Look and Feel
• Java employs a pluggable look and feel
(PLAF) model to control the appearance of
a UI.
• You can set the LAF in your program or use
getSystemLookAndFeelClassName()
method for consistency.
• You can write your own LAF by
implementing the abstract class
LookAndFeel and utilizing methods from the
javax.swing.plaf package.
4
Look and Feel
• Three main LAFs are built into
javax.swing.plaf:
– Windows windows.WindowsLookAndFeel
– Motif motif.MotifLookAndFeel
– Metal (cross platform)
• Set LAF using
UIManager.setLookAndFeel("com.sun.j
ava.swing.plaf.<LAF>");
Demo LookAndFeel.java Pages 1374 - 1375.
3
5
Look and Feel
• You can override the default look and feel
at the command line:
java -Dswing.defaultlaf=
com.sun.java.swing.plaf.
motif.MotifLookAndFeel
<applicationname>
6
Swing and Concurrency
• Recall last time, we cautioned against
running a GUI through a run() method.
• Watch what happens when this executes:
Demo LongRunningTask.java Pages 1382 - 1383.
...
public static void main(String[] args) {
run(new LongRunningTask(), 200, 150);
... // LongRunningTask is a JFrame
4
7
Swing and Concurrency
• The listener on Button b2 has no effect
because the event dispatch queue is busy with
the long running task:
So what can be done?
...
b2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt
) {
Thread.currentThread().interrupt()
;}});
...
8
Swing and Concurrency
• Why not try instantiating en ExecutorService
and just shutting down the service to stop
execution?
...
ExecutorService executor =
Executors.newSingleThreadExecutor();
...
b2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
executor.shutdownNow(); // Heavy-handed
}});
Demo InterruptableLongRunningTask.java
Pages 1382 - 1383.
5
9
Swing and Concurrency
• What happens when we try to stop
and start the task in
InterruptableLongRunningTask
more than once? Why?
Demo InterruptableLongRunningTask.java
Pages 1382 - 1383.
10
Swing and Concurrency
• The Callable/Future feature in Java 5 is what
we need.
• Eckel defines a TaskManager class having
tuples to hold the Callable and the Future that
comes back from it.
• The tuple allows us to keep track of the original
task from which we can get information not
returned by Future.
Demo InterruptableLongRunningCallable.java
on Pages 1386 - 1387.
6
11
Swing and Concurrency
• Be sure to give your user feedback if you ever
need to execute a long-running background
task.
– Examples of long-running tasks include database
queries, and network-intensive processes.
• The MonitoredLongRunningCallable
program gives you a framework that you can
build upon.
Demo MonitoredLongRunningCallable.java on
Pages 1390 - 1391.
12
Java Beans
• The "visual programming" paradigm involves:
– A canvass upon which you can place
components.
– A palette of components whose presentation
you control through their properties.
– Behaviors that are connected to the
components.
• Key idea: The IDE interrogates the
components to determine the properties are
available to you.
7
13
Java Beans
• You can write your own Java Beans for use in
IDEs by following the naming conventions.
• When you do this, the Bean properties that you
need to expose to the IDE will be properly
flagged.
• These properties and methods can be
interrogated by an Introspector (in addition to
the IDE).
Demo BeanDumper.java on Pages 1398 - 1399.
14
Java Beans
• For a property named xxx, you need to
have getXxx() and setXxx() methods.
• A boolean xxx, may have isXxx().
• Ordinary methods must be public.
• If you write a customized event, xxx,
you'll need addXxxxxListener() and
removeXxxListener() methods.
8
15
Java Beans
• You can never be sure how your bean will be
used.
• If it's in a multithreaded environment, most
methods should be synchronized.
• If more than one listener is interested in an
event, e.g., MouseListener and
MouseMotionEvent, you must synchronize
the methods, otherwise the results are
unpredictable.
16
Java Beans
• A Bean needs to be packaged before a Bean-
enabled IDE can use it.
• The package is a jar file consisting of all of the
Bean components and a manifest entry
indicating that the jar contains a Bean.
– Your Bean should reside inside its own
package.
– The manifest entry must include the package
name.
9
17
Java Beans
• This is the manifest format:
• Package it up with:
Manifest-Version 1.0
Name: <package>/<mainclass>.class
Java-Bean: True
jar cfm <beanname>.jar
<manifestname>.mf <packagename>
18
Swing Alternatives
• Swing is fine for use in the enterprise.
– Desktops are controlled by the enterprise.
• Swing is seen as too heavyweight for Web
applications
• Applets are little better.
– You don't have control over the desktop--
or even the browser.
• For Internet-based applications, a number
of alternatives are gaining traction.
10
19
Swing Alternatives
• The 800 pound gorilla in the game is Adobe
with its Macromedia Flex Flash solution.
– Just about every browser contains a Flash
player.
– The plug-in is a small download.
• The IDE comes with a component library.
• You program the components using MXML.
– Macromedia XML.
See Pages 1416 - 1447.
20
Swing Alternatives
• AJAX (Asychronous JavaScript and XML)
– A set of native client-side browser
technologies that can be combined to build
powerful user interfaces and asynchronous
communication with server software.
• Uses the native browser scripting language:
No plug-ins required.
– Gaining a great deal of support from major
tool vendors,
11
21
Swing Alternatives
• AJAX Disadvantages:
• Complex: sophisticated development
can be difficult.
• Security has not been ironed out.
22
Swing Alternatives
• SWT (Standard Widget Toolkit)
• Free GUI library from www.eclipse.org.
(IBM created it.)
• Provides responsive user experience.
• Virtual machine is for Windows only.
– Probably Linux soon.
12
23
Swing Alternatives
• SWT (Standard Widget Toolkit)
• Free GUI library from www.eclipse.org. (IBM
created it.)
• Virtual machine is for Windows only.
– Probably Linux soon.
• SwingWT
• Free from swingwt.sourceforge.net.
• Provides a Swing compatible API, but uses
SWT for the implementation.
24
Swing Alternatives
• BambooKit
• www.bambookit.com. Light-weight XML-
based GUI good for small devices.
• Zaval
• www.zaval.org. Another light-weight GUI.
Certainly, many more will
emerge in the near future.
13
25
Summary
• Swing look and feel can be controlled by
your program.
• Java Swing concurrency is tricky. Take
advantage of your author's TaskManager
program as a template.
• Java Beans allow you to create and
distribute custom graphical components.
• There are many alternatives to Swing,
with more sure on the way.