comp.lang.java.gui FAQ
1. Introduction
~~~~~~~~~~~~~~~
Q.1.1. What is this, and what does it contain?
This is the FAQ for the comp.lang.java.gui newsgroup. It mostly
consists of a selection of pointers to resources regarding Swing and
AWT programming in Java.
Q.1.2. There are so many Java FAQs. Which is the right, official one?
There is probably not THE FAQ. Everyone can start an FAQ, and many have
done so. See it as some benefit. There is a lot of information out
there.
For a list of other FAQs and FAQ lists :-) see
http://mindprod.com/jgloss/faqs.html
Q.1.3. I noticed broken links in the FAQ. Don't you verify them before
publishing?
No, I don't. I rely on feedback from readers. Also, links might break
at any time, e.g. just seconds after a link has been verified.
Q.1.4. What is AWT?
AWT (The Abstract Window Toolkit) is Sun's first Java GUI toolkit. It
is rather limited and uses the native GUI components of the operating
system.
Unless you have to support an old VM, Swing is usual the better choice
for a Java GUI toolkit.
Q.1.5. What is Swing?
Swing is Sun's second attempt at a Java toolkit. It is rich in
functions and widgets, and is considered the standard Java GUI
toolkit. Nowadays it is bundled with the Java 2 Standard Edition.
Most parts of Swing are written in Java, especially, most of the GUI
components. Swing uses some parts of AWT in order to gain access to the
native GUI system for event handling and top-level containers.
Q.1.6. What is SWT
SWT is an alternative GUI toolkit from IBM. Unlike AWT and Swing, it is
not part of the Java 2 Standard Edition. You have to obtain it
separately for the platforms you want to support (it uses a native
library).
2. The Top 5 Questions
~~~~~~~~~~~~~~~~~~~~~~
Q.2.1. My GUI freezes. What to do?
Most likely you are blocking the event dispatching thread (EDT).
Offload time-consuming tasks from your event listeners to separate
threads.
You can do the necessary implementation by hand, or you can use
existing frameworks like the SwingWorker class from Sun. See the series
of articles in
http://java.sun.com/products/jfc/tsc/articles/threads/threads1.html
Q.2.2. How do I update the GUI from another thread, e.g. once I have
offloaded a time consuming task from the EDT to another thread?
See the
javax.swing.SwingUtilities.invokeLater()
and
javax.swing.SwingUtilities.invokeAndWait() methods.
Usually you want invokeLater().
Again, see
http://java.sun.com/products/jfc/tsc/articles/threads/threads1.html
for more details.
Q.2.3. I have arranged all my widgets nicely on a window. Then I changed
the OS / Java version / font / PLAF. Now everything is broken. What's
going on?
This sounds as if you don't use layout managers, but instead hard-coded
component sizes and widgets. If you want to avoid this problem, there
is no way around using layout managers, or implementing your own
geometry management from scratch.
Q.2.4. My graphics on a Component (Canvas, JPanel, JComponent, etc.)
gets corrupted, or I get a null pointer exception when trying to draw
on a component. How can I avoid this?
Do not use Component.getGraphics(). Instead, subclass and override
the paint() (AWT), or paintComponent() (Swing) method.
Component.getGraphics() simply doesn't work. Java uses a callback
mechanism for drawing graphics. You are not supposed to "push" graphics
information into a component using getGraphics(). Instead you are
supposed to wait until Java calls your paint()/paintComponent() method.
At that moment you are supposed to provide the Component with the
drawings you would like to do.
In addition, there are situations where Component.getGraphics()
simply returns null. This is a defined behavior of the method.
See
http://java.sun.com/products/jfc/tsc/articles/painting/index.html
for more information.
Q.2.5. How can I make a transparent or non-rectangular windows?
You can't in a good, platform independent way.
Although particular Java components can be 'transparent', they will
always be contained inside a rectangular root component that is not
transparent.
One hack is to take a snapshot of the underlying screen region using
java.awt.Robot.createScreenCapture(rectangle). And then using that
snapshot as a background image for the window. If the background
changes, the illusion is gone.
[This has been demonstrated by Linda Radecke. However, her site and
the example is now gone from the web. If someone still has the code,
or a pointer to the code, please feel free to submit it.]
An alternative for applets can be found here:
http://java.sun.com/docs/books/faq/src/app/MatchBackgroundExample.html
[I also remember to have seen a solution using JNI for windows.
Pointers are of course welcome.]
3. Other Common Questions
~~~~~~~~~~~~~~~~~~~~~~~~~
Q.3.1. My GUI has rendering problems when the JMenu opens over my
top Panel ...
Swing (JMenu) and AWT (Panel) components do not mix well.
See
http://java.sun.com/products/jfc/tsc/articles/mixing/index.html
for things you have to pay attention, too.
4. Resources
~~~~~~~~~~~~
There are many good on-line resources regarding Java GUI programming
out there. Here is an incomplete list:
[Note: I try to limit the list to resources which get recommended often
on c.l.j.g, including resources from regulars]
4.1. Sun's Java Web Site
Sun's Swing Quick-Start Tutorial:
http://java.sun.com/docs/books/tutorial/uiswing/mini/index.html
Sun's Swing Tutorial:
http://java.sun.com/docs/books/tutorial/uiswing/index.html
Sun's Swing Connection (TSC) (Swing developer's site):
http://java.sun.com/products/jfc/tsc/index.html
The TSC article index (Swing architecture, Swing and threads, painting
architecture, etc.):
http://java.sun.com/products/jfc/tsc/articles/
4.2. Icons
Sun's well hidden set of Icons for Swing's Metal LnF:
http://developer.java.sun.com/developer/techDocs/hi/repository/
More icons:
http://sourceforge.net/projects/icon-collection/
(the link to javalobby.org is broken)
4.3. Misc. Examples, Tips and Tricks
A collection of examples for doing nice things with Swing components
(some are a little bit outdated):
http://www2.gol.com/users/tame/swing/examples/SwingExamples.html
Christian Kaufhold's Java and Swing info (including JTable info):
Jeanette's notes (including JTable remarks):
http://www.mycgiserver.com/~Kleopatra/swing/swingentry.html
Marco Schmidt's Java resource page (Java imaging information)
http://www.geocities.com/marcoschmidt.geo/java.html
Karsten Lentsch's company web page:
4.4. Style Guides
Java Look and Feel Design Guidelines:
http://java.sun.com/products/jlf/ed2/book/index.html
Java Look and Feel Design Guidelines: Advanced Topics:
http://java.sun.com/products/jlf/at/book/index.html
4.5. SDK Documentation
GUI Information in the SDK documentation (commonly overlooked):
See your local SDK installation, or
Abstract Window Toolkit (AWT)
http://java.sun.com/j2se/1.4.2/docs/guide/awt/index.html
Swing
http://java.sun.com/j2se/1.4.2/docs/guide/swing/index.html
2D Graphics and Imaging
http://java.sun.com/j2se/1.4.2/docs/guide/2d/index.html
Image I/O
http://java.sun.com/j2se/1.4.2/docs/guide/imageio/index.html
Print Service
http://java.sun.com/j2se/1.4.2/docs/guide/jps/index.html
Input Method Framework
http://java.sun.com/j2se/1.4.2/docs/guide/imf/index.html
Accessibility
http://java.sun.com/j2se/1.4.2/docs/guide/access/index.html
Drag-and-Drop data
http://java.sun.com/j2se/1.4.2/docs/guide/dragndrop/index.html
Swing Examples in the SDK:
See the directory demo/jfc in your Java SDK installation.
4.6. More Swing
Swing class-hierarchy chart
http://www.holub.com/goodies/java.swing.html
jGuru Swing FAQ
http://www.jguru.com/faq/Swing
CodeGuru Swing Examples
http://www.codeguru.com/java/Swing/index.shtml
4.7. Online Magazines
Regular GUI articles can be found in
4.8. Java 2D API
The Programmer's Guide
http://java.sun.com/j2se/1.4.2/docs/guide/2d/spec/j2d-bookTOC.html
4.9. AWT
Sun's AWT FAQ
http://java.sun.com/docs/books/faq/faqawt.html
4.10. Java 3D API
There is a separate newsgroup. see
news:comp.lang.java.3d
http://groups.google.com/groups?group=comp.lang.java.3d
4.11. General Java
The Java Tutorial:
http://java.sun.com/docs/books/tutorial/
All kinds of tutorials:
http://developer.java.sun.com/developer/onlineTraining/
Roedy's Java Glossary:
http://www.mindprod.com/jgloss/jgloss.html
The (more and more outdated) Java Programmer's FAQ:
4.12. Newsgroup
This group's archive at Google:
http://groups.google.com/groups?group=comp.lang.java.gui
Making the most out of a newsgroup (ignore the hacker slang):
http://www.catb.org/~esr/faqs/smart-questions.html
4.13. More?
Q.4.13.1. But I need more, more, more!
Learn how to use a search engine like
5. Improvement Suggestions
~~~~~~~~~~~~~~~~~~~~~~~~~~
Please mail suggestions, corrections, updates, etc. to cljg_faq on the
gmx.de site. Your "Subject:" line must contain the string "[cljg]"
somewhere. Otherwise your mail will be discarded automatically. In
addition, the address is heavily spam-protected.
If you suggest a new entry, please also provide the answer, not only
the question.
6. Acknowledgments
~~~~~~~~~~~~~~~~~~~
This FAQ contains contributions and help from:
Andrew Thompson, David Postill
--