Download Graphical User Interfaces in Java Part 2

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts
no text concepts found
Transcript
Graphical User Interfaces in Java
Part 2
COMP440
Penn State Harrisburg
April 17, 2007
© Julia M. Lobur
1
Objectives
• Illustrate Swing look-and-feel.
• Demonstrate the relationship between
concurrency and Java Swing.
• Present Java Beans.
• Discuss alternatives to Swing.
2
1
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.
3
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.
4
2
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>
5
Swing and Concurrency
• Recall last time, we cautioned against
running a GUI through a run() method.
• Watch what happens when this executes:
...
public static void main(String[] args) {
run(new LongRunningTask(), 200, 150);
... // LongRunningTask is a JFrame
Demo LongRunningTask.java Pages 1382 - 1383.
6
3
Swing and Concurrency
• The listener on Button b2 has no effect
because the event dispatch queue is busy with
the long running task:
...
b2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
Thread.currentThread().interrupt()
;}});
...
So what can be done?
7
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.
8
4
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.
9
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.
10
5
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.
11
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.
12
6
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.
13
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.
14
7
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.
15
Java Beans
• A Bean needs to be packaged before a Beanenabled 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.
16
8
Java Beans
• This is the manifest format:
Manifest-Version 1.0
Name: <package>/<mainclass>.class
Java-Bean: True
• Package it up with:
jar cfm <beanname>.jar
<manifestname>.mf <packagename>
17
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.
18
9
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.
19
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,
20
10
Swing Alternatives
• AJAX Disadvantages:
• Complex: sophisticated development
can be difficult.
• Security has not been ironed out.
21
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.
22
11
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.
23
Swing Alternatives
• BambooKit
• www.bambookit.com. Light-weight XMLbased GUI good for small devices.
• Zaval
• www.zaval.org. Another light-weight GUI.
Certainly, many more will
emerge in the near future.
24
12
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.
25
13