Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

comp.lang.java.gui FAQ

13 views
Skip to first unread message

Thomas Weidenfeller

unread,
Jan 14, 2004, 7:13:59 AM1/14/04
to
Version: $Revision: 1.6 $
Posting-Frequency: monthly
Copyright: Copyright (c) 2003, Thomas Weidenfeller
Maintainer: Thomas Weidenfeller. See below for mailing instructions.
Last-modified: $Date: 2004/01/14 12:10:04 $

comp.lang.java.gui FAQ

Table of Contents

1. Introduction
Q.1.1. What is this, and what does it contain?
Q.1.2. There are so many Java FAQs. Which is the right, official one?
Q.1.3. I noticed broken links in the FAQ. Don't you verify them before
publishing?
Q.1.4. What is AWT?
Q.1.5. What is Swing?
Q.1.6. What is SWT?

2. The Top 5 Questions
Q.2.1. My GUI freezes. What to do?
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?
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?
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?
Q.2.5. How can I make a transparent or non-rectangular windows?

3. Other Common Questions
Q.3.1. My GUI has rendering problems when the JMenu opens over my
top Panel ...
Q.3.2. I append text to a JTextArea. How Do I make sure the text area is
always scrolled down to the end of the text?

4. The Newsgroup
Q.4.1. Where can I find an archive of this group?
Q.4.2. What is an SSCCE?
Q.4.3. Why don't people like top-posting? What is top-posting?
Q.4.4. Is there more about posting to newsgroups and asking questions?

5. Resources
5.1. Sun's Java Web Site
5.2. Icons
5.3. Misc. Examples, Tips and Tricks
5.4. Style Guides
5.5. SDK Documentation
5.6. More Swing
5.7. Online Magazines
5.8. Java 2D API
5.9. AWT
5.10. Java 3D API
5.11. General Java
5.12. More?
Q.5.12.1. But I need more, more, more!

6. Improvement Suggestions

7. Acknowledgments


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.

Q.3.2. I append text to a JTextArea. How Do I make sure the text area is
always scrolled down to the end of the text?

textarea.setCaretPosition(textarea.getDocument().getLength());

4. The Newsgroup
~~~~~~~~~~~~~~~~

Q.4.1. Where can I find an archive of this group?

See e.g. http://groups.google.com/groups?group=comp.lang.java.gui

Q.4.2. What is an SSCCE?

Short/small, self contained, compilable, example (source code).

It would be best if you provide such example source code in our first
posting. When asked for one, please don't complain that your source
code is to large, to tricky, to secret for being cut down to a
reasonable size and posted. You have the problem, and you asked in a
public forum, so it is in your interest to provide the requested
information.

Q.4.3. Why don't people like top-posting? What is top-posting?

See the following Question & Answer (aehm, Answer & Question) section:

A: Because it messes up the order in which people normally read text.
Q: Why is top-posting such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on Usenet and in e-mail?

-- Common Usenet
signature

Q.4.4. Is there more about posting to newsgroups and asking questions?

Yes, see e.g.

http://www.catb.org/~esr/faqs/smart-questions.html

for making the most out of a newsgroup (ignore the hacker slang): [The
autor of that document has requested a notice that he is not a help
desk for your problems].

Also see the newsgroups:

news:news.newusers.questions
news:news.answers


5. 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]


5.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/


5.2. Icons

It is suggested to check the licenses of each icon collection before
using them in any products. Several of the following packages come with
open source / free software licenses.

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 on that page link to javalobby.org is broken)

The Ximian Open-Office icons (very nice):
http://developer.ximian.com/themes/icons/ooo-icons.html

Gnome icons:
http://tigert.gimp.org/gnome/gnome-stock/

SVG BlueSphere Icon Theme:
http://svgicons.sourceforge.net/

KDE Icons:
http://www.buzzard.org.uk/jonathan/kde-icons.html

5.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):

http://www.chka.de/

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:

http://www.jgoodies.com/


5.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


5.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.


5.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


5.7. Online Magazines

Regular GUI articles can be found in

http://www.javaworld.com/


5.8. Java 2D API

The Programmer's Guide

http://java.sun.com/j2se/1.4.2/docs/guide/2d/spec/j2d-bookTOC.html


5.9. AWT

Sun's AWT FAQ

http://java.sun.com/docs/books/faq/faqawt.html


5.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

5.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:

http://www.afu.com/intro.html

5.12. More?

Q.5.12.1. But I need more, more, more!

Learn how to use a search engine like

http://www.google.com


6. 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.


7. Acknowledgments
~~~~~~~~~~~~~~~~~~

This FAQ contains contributions and help from:

Andrew Thompson, David Postill

Andrew Thompson

unread,
Jan 14, 2004, 12:20:32 PM1/14/04
to
"Thomas Weidenfeller" <nob...@ericsson.invalid> wrote in message
news:bu3bnc$c0v$1...@newstree.wise.edt.ericsson.se...
....

| Q.4.2. What is an SSCCE?
|
| Short/small, self contained, compilable, example (source code).
.....
For more information and hints on how to make...
http://www.physci.org/codes/sscce.jsp

[ It also has a link to the Java On-line Compiler,
which certainly encourages 'short' since it only
compiles code up to 10Kb.. ;-) ]
.....


| 7. Acknowledgments
| ~~~~~~~~~~~~~~~~~~
|
| This FAQ contains contributions and help from:
|
| Andrew Thompson, David Postill

Thanks!

--
Andrew Thompson
* http://www.PhySci.org/ PhySci software suite
* http://www.1point1C.org/ 1.1C - Superluminal!
* http://www.AThompson.info/andrew/ personal site


0 new messages