Swingapps

Solo disponible en BuenasTareas
  • Páginas : 9 (2093 palabras )
  • Descarga(s) : 7
  • Publicado : 30 de julio de 2010
Leer documento completo
Vista previa del texto
Developing Swing Applications
Lee Chuk Munn
Staff Engineer chuk-munn.lee@sun.com
1

The Swing Superlatives

Too Big Too Slow Too Complex Too Ugly
2

Swing Application
•Reality
>Basic widget is not really that different from AWT >Complex widget has a different programming model


Misuse of these have lead to poor performance Eg. centering a window on the screen

>Should haveincluded default behaviour but did not


•Swing has regained traction in recent years
>Huge performance increases >OSS SwingX to deliver cool features

3

Swing Today
•JavaSE 6u10
>Fast, takes advantage to hardware acceleration >New look and feel – Nimbus


Extremely customizable

>Translucent and shaped windows >Updated Java Plugin architecture for Swing delivery

•Excellenttools
>NetBeans Matisse GUI builder

•Lots of expertise
>Books from beginners, HOW-TOs, best practices >Online resources – forums, blogs, OSS projects

•Beginning to loose that ugly/difficult stigma
4

Better User Experience
5

Low Hanging Fruits
•Set a default action to a frame
myFrame.getRootPane() .setDefaultButton(someButton);

•Set focus on a particular field
firstFieldInFrame.requestFocusInWindow(true);

•Set the frame icon!
>Distinguish your application
ClassLoader
cl
=
this.getClass().getClassLoader(); myFrame.setImageIcon(ImageIO.read( cl.getResourceAsStream(“images/img.png”)));
6

Positioning the Frame
•I really hate having to move the frame from top left corner to the center
>Swing should have center it as default >JOptionPane does this – inconsistentbehaviour

•Centering a window
>frame.setLocationRelativeTo(null)

•Positioning at a particular location
//Position
a
frame
a
the
right
side
of
the
screen Dimension
sSz
= Toolkit.getDefaultToolkit.getScreenSize(); Dimension
fSz
=
frame.getSize(); frame.setLocation(sSz.width
–
fSz.width
–
50,
50);
7

Accelerator Keys
•Jump to a particular field by using the keyboard instead of having touse the mouse to click it •Extremely easy to set in NetBeans •Programatially
//Create
a
label JLabel
nameLabel
=
new
JLabel(“Name”); nameLabel.setDisplayedMnemonic('n'); //Create
a
textfield JTextField
nemeField
=
new
JtextField(); //Associate
the
label
to
the
textfield nameLabel.setLabelFor(nameField);
8

EDT and Processing
•Eg. in an event handler – actionPerformed() •Never perform heavyduty processing in EDT
>Will cause the GUI to appear sluggish or unresponsive

•Two types of processing
>Update the UI in response to a widget


SwingUtilities.invokeLater() ●Executes on EDT thread after all other pending AWT events

>Performing some processing long lived process
● ● ●

Extend SwingWorker

More heavyweight than SwingUtilities.invokeLater()

Benefit over Thread arelifecycle management, property change support, progress bar, etc.
9

Working with BufferedImages
•Types of Images
>Toolkit, VolatileImage, BufferedImage

•BufferedImage the most versatile
>Speed, manipulation >Extremely easy to load with imageio package

•Creating BufferedImage
BufferedImage
img
=
 new
BufferedImge(100,
100,
TYPE_INT_ARGB);

•Loading into a BufferedImageInputStream
is
=

ClassLoader
loader
=
this.getClass().getClassLoader(); loader.getResourceAsStream(“some/image.png”); BufferedImage
img
=
ImageIO.read(is);
10

UI Update from EDT
•Best to let other events on EDT to complete first •invokeLater() executes run() on EDT after all other pending events have been processes
//Update
the
status
bar
-
Runnable public
void
run()
{ statusBar.setText(someText); }public
void
actionPerformed(ActionEvent
aEvt)
{ SwingUtilities.invokeLater(this); }
11

Splash Screen
12

JDK 6 Splash Screen Support
•Very easy to display splash screen either from command line or from JAR file •From command line
java
-splash:my_splash.gif
MyApp

•From within a JAR file – with Main-Class attribute • Splashscreen-Image:
my_splash.gif •Splash screen will close
>...
tracking img