[Comp.Sci.Dept, Utrecht] Note from archiver<at>cs.uu.nl: This page is part of a big collection of Usenet postings, archived here for your convenience. For matters concerning the content of this page, please contact its author(s); use the source, if all else fails. For matters concerning the archive as a whole, please refer to the archive description or contact the archiver.

Subject: comp.lang.java.gui FAQ

This article was archived around: 16 Nov 2006 05:43:55 GMT

All FAQs in Directory: computer-lang/java/gui
All FAQs posted in: comp.lang.java.gui, comp.lang.java.programmer
Source: Usenet Version


Archive-name: computer-lang/java/gui/faq Version: $Revision: 1.20 $ Posting-Frequency: monthly Copyright: Copyright (c) 2003 - 2006 Thomas Weidenfeller Maintainer: Thomas Weidenfeller. See below for mailing instructions. Last-modified: $Date: 2006/06/09 15:03:56 $
comp.lang.java.gui FAQ ________________________________________________________________________ Table of Contents PART I ================================================================= SECTION 1 - Introduction Q1.1 What is this, and what does it contain? Q1.2 What's not in here? Q1.3 Where do I find a copy of the FAQ? Q1.4 There are so many Java FAQs. Which is the right, official one? Q1.5 Does Sun support or endorse this FAQ? Q1.6 I noticed broken links in the FAQ. Don't you verify them before publishing? Q1.7 Is there an HTML, Word, <whatever format> version of the FAQ? Q1.8 What is AWT? Q1.9 What is Swing? Q1.10 What is SWT? SECTION 2 - The comp.lang.java.gui Newsgroup Q2.1 What is the newsgroup's charter? What are acceptable topics? Q2.2 Which topics or posting are not welcome in the newsgroup? Q2.3 Where can I find an archive of the newsgroup? Q2.4 What is an SSCCE? Q2.5 Why don't people like top-posting? What is top-posting? Q2.6 Is there more about posting to newsgroups and asking questions? Q2.7 Does Sun support or endorse the newsgroup? SECTION 3 - The Top 5 Questions Q3.1 My GUI freezes or doesn't update. What to do? Q3.2 How do I update the GUI from another thread? Q3.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? Q3.4 My graphics on a Canvas/JPanel/JComponent, etc. gets corrupted, or I get a null pointer exception when trying to draw. How can I avoid this? Q3.5 How to create a transparent or non-rectangular window? SECTION 4 - Architecture Q4.1 What is this Model-View-Controller (MVC) stuff? Q4.2 What is the Swing single-threading issue? Q4.3 What is the right way to start a Swing GUI? Q4.4 What is full-screen exclusive mode? Q4.5 What is active rendering? PART II ================================================================ SECTION 5 - Window / [J]Frame / [J]Dialog (Top-Level Containers) Q5.1 How can I ensure a window is always on top of all other windows using AWT or Swing? Q5.2 How can I (de)iconify a window? Q5.3 How can I replace/remove the icon in the title bar (window decoration) of a [J]Frame? Q5.4 How to replace the icon in the title bar (window decoration) of a [J]Dialog? Q5.5 My modal dialog goes behind the main window. How can I ensure it is in front instead? Q5.6 How to bind the escape key to the JDialog cancel operation? Q5.7 How can I implement my own JFrame/JDialog close handling? Q5.8 How Do I center a window on the screen? How do I get the screen size? Q5.9 How to ensure a minimum or maximum window size? Q5.10 How to ensure a particular aspect ration of a window? Q5.11 How can I delegate the window placement to the window system or manager? Q5.12 I need to take some toolbar (dock, panel) size into account when calculating a window position an/or size. How? Q5.13 My window layout is displayed incorrectly. I have to move the window, before the layout is right. What's wrong? Q5.14 How can I display a Splash Screen at the start of my Program? SECTION 6 - [J]Component (Widgets) 6.1 General Questions Q6.1.1 How do I position components (widgets) on a window? Q6.1.2 How to create a transparent widget? Q6.1.3 How to create a non-rectangular widget? Q6.1.4 What are Insets? Q6.1.5 How do I find a component's top-level container (e.g. the window)? 6.2 JTree Q6.2.1 I changed the data / structure for my JTree, but the display doesn't get updated. What's going on? Q6.2.2 How do I set a custom icon for a node? Q6.2.3 How do I remove all my nodes from a JTree at once? 6.3 Styled Text / JEditorPane / JTextPane Q6.3.1 Can I use RTFEditorKit to read RTF documents created by Word? Q6.3.2 I have problems using the Swing HTML parser to parse all kinds of HTML. Is this normal? Q6.3.3 Some of my CSS styles don't work out. Is this normal? Q6.3.4 Can I use Swing's HTML support to write a web browser? Q6.3.5 Can I use Swing's HTML support to build an on-line help system or e-book? Q6.3.6 If HTML support is really so broken in Java, what is it good for? 6.4 [J]TextArea Q6.4.1 I append text to a JTextArea. How to ensure the text area is always scrolled down to the end of the text? Q6.4.2 How to use several different fonts (styles, sizes) in one [J]TextArea? 6.5 [J]Label / [J]Button Q6.5.1 How can I have multiple Lines in a [J]Label? Q6.5.2 I want to have a hyperlink in a [J]Label. How can I do this? Q6.5.3 How do I make a JButton the default button in a JDialog? SECTION 7 - JScrollPane Q7.1 I added a component (e.g. a JPanel with an image) to a scrollpane, but the scrollpane doesn't show it at the right size / without scrollbars, etc. What's wrong? PART III =============================================================== SECTION 8 - Graphics & Painting Q8.1 What is the equivalent of AWT's Canvas in Swing? Q8.2 When drawing on a JPanel, the background is garbled. Q8.3 How do I generate some charts / plots in Java? Q8.4 How to draw some graphs in Java? Q8.5 I want to write a diagram editor. Where to start? Q8.6 How do I draw lines between JLabels on a JPanel? Q8.7 How to debug graph painting? Q8.8 I need to draw a tree. How? Q8.9 I need an algorithm for drawing ... Q8.10 When I subclass JPanel/JComponent, I need to override paint(), right? Q8.11 Why does drawImage() fail when I try to display a loaded image? Why are the width and height of my loaded image both zero? Q8.12 How do I resize (zoom in/out) my Graphics? Q8.13 Where do I find the icons which are used by Swing itself? Q8.14 Where do I find typical application icons? SECTION 9 - Fonts Q9.1 Which Fonts can I use? Can I use font <xyz>? Q9.2 How to turn on text antialiasing in Swing? Q9.3 Why do some of my characters get displayed as squares? SECTION 10 - Other Common Questions Q10.1 My GUI has rendering problems when the JMenu opens over my top Panel ... Q10.2 Can I use Swing for Applets? Q10.3 How do I change a color/font/etc. globally for an application? Q10.4 How do I get all the UIDefaults, and what do they mean? Q10.5 Why is Swing so slow? SECTION 11 - Non-GUI Questions Q11.1 How do I do a text/console UI in Java? Q11.2 How can I do this JavaScript thing on my web site? Q11.3 I want to make ... SECTION 12 - Resources 12.1 Sun's Java Web Site 12.2 Other Sun Sites 12.3 Icons 12.4 Miscellaneous Examples, Tips and Tricks 12.5 Style Guides 12.6 SDK Documentation 12.7 More Swing 12.8 Online Magazines 12.9 Java 2D API 12.10 Java 3D API 12.11 General Java 12.12 More? Q12.12.1 But I need more! SECTION 13 - Improvement Suggestions SECTION 14 - Acknowledgments ________________________________________________________________________ PART I ======================================================================== SECTION 1 - Introduction ~~~~~~~~~~~~~~~~~~~~~~~~ Q1.1 What is this, and what does it contain? This is the FAQ for the comp.lang.java.gui newsgroup. It mostly deals with Java Standard Edition Swing issues, and contains some AWT information, too. In many cases these are also topics many readers would like NOT to see discussed again soon. Q1.2 What's not in here? This is not a general introduction to programming, Java programming, or GUI programming. Further it is assumed that the reader is familiar with Java and GUI terminology. Also, the following topics are either not covered at all or just cursory touched, since they are not often discussed in c.l.j.g, have own groups, and/or the FAQ author is not knowledgeable about them: * J2ME GUI programming * Java 3D programming (see <news:comp.lang.java.3d>) * Java game programming * Computer graphics algorithms (see <news:comp.graphics.algorithms>) Q1.3 Where do I find a copy of the FAQ? 1) The FAQ is regularly posted to <news:comp.lang.java.gui> 2) At Usenet archives like <http://gd.tuwien.ac.at/faqs/faqs-hierarchy/comp/comp.lang.java.gui/> <ftp://ftp.cs.uu.nl/pub/NEWS.ANSWERS/computer-lang/java/gui/> <http://www.uni-giessen.de/faq/archiv/computer-lang.java.gui.faq/> 3) Just search an archive like <http://groups.google.com/groups?group=comp.lang.java.gui> 4) With the author's permission people have placed (sometimes older) versions of the FAQ on web sites, too, e.g. <http://mindprod.com/jgloss/guifaq.html> <http://www.physci.org/guifaq.jsp> 5) There is even a Japanese translation around: <http://homepage1.nifty.com/algafield/JavaGUIFaq19j.html> Q1.4 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 (and many have stopped after a few weeks). Q1.5 Does Sun support or endorse this FAQ? No, it is just a newsgroup FAQ. Sun probably doesn't know about it. NOTE: The author of this FAQ does not have any inside information or contacts to Sun's Java or GUI development team and has never been contacted by Sun. Therefore, the FAQ's author is not in a position to forward suggestions to Sun or help with expedited answers from Sun. Please refrain from such requests. See also: "Q2.7 Does Sun support or endorse the newsgroup?" Q1.6 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. Q1.7 Is there an HTML, Word, <whatever format> version of the FAQ? There is no such official version. Some people went through the effort to convert the FAQ to HTML. I suggest to use the <http://txt2html.sourceforge.net> software to create am HTML version for personal usage. See also: "Q1.3 Where do I find a copy of the FAQ?" Q1.8 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. Q1.9 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. It is build on AWT's lightweight component framework. Q1.10 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). SECTION 2 - The comp.lang.java.gui Newsgroup ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Q2.1 What is the newsgroup's charter? What are acceptable topics? The voting for the group with the group's charter passed on 1997-04-10: <http://groups.google.com/groups?selm=860665862.3170%40isc.org> A longer history of comp.lang.java.* reorganizations can be found in <ftp://ftp.isc.org/usenet/news.announce.newgroups/comp/comp.lang.ja va-reorg> Here is an excerpt from the '97 reorg charter (Note, "all groups" refers to all the Java groups from that voting, including comp.lang.java.gui): CHARTER: all groups The normal practice should be that most articles are posted to one single, correct group ONLY. Cross-posting is only appropriate when the problem is hard to categorize or when it legitimately concerns more than one group. Answers should be posted to a single group only once the nature of the problem has been ascertained. Many articles of this sort should go to comp.lang.java.help (only). It is not appropriate to post binary class files or long (longer than one or two screenfuls) source listings on any of these groups. Instead, the post should reference a WWW or FTP site (short source snippets to demonstrate a particular point or problem are fine). END CHARTER. [...] CHARTER: comp.lang.java.gui This unmoderated group is for any and all discussion relating to GUI toolkits or window frameworks in Java. Topics include the AWT, Netscape's IFC, Microsoft's planned AFC, Visix's Vibe toolkit, among others. The newsgroup will also be the appropriate place for discussion of the JDK event model, mouse and keyboard issues, bugs in windowing code, and graphics programming in Java. If it concerns something that can be seen on the screen, it belongs in this group. END CHARTER. One will note the list of ancient Java GUI technologies, and the absence of Swing. It is safe to say, that nowadays most discussions are about Swing, plus a few about AWT and Java printing. Q2.2 Which topics or posting are not welcome in the newsgroup? Of course, this question is never asked, but over the time, it has turned out that certain types of postings are not welcome, even if Java related. This includes: * Issues answered in this FAQ are issues most posters don't want to see discussed again soon. * Posting your homework. * Advocacy. Goto comp.lang.java.advocacy instead. * Postings urging the readers to help. Especially in conjunction with whining. No one in the group is paid to help you. No one owns you anything. * Public and hidden advertising. You think you are too clever to be caught? Well, read this thread in our sister group c.l.j.programmer first, and watch how some business lost all its reputation: http://groups.google.com/groups?threadm=58882783. 0307010403.67c54a5f%40posting.google.com * Postings which just contain a statement like "Help! It does not work!", without any additional information, like the exact error message, source code, or even a hint what "it" is supposed to mean. * Postings demonstrating unwillingness to learn are not welcome, too. And learning starts by reading the API documentation before posting. * Test messages are not welcome. Instead, use alt.test.*, and learn how newsgroups work. * Postings written in "elite hacker" style or txt message/chat style. See also: "Q2.4 What is an SSCCE?" Q2.3 Where can I find an archive of the newsgroup? See e.g. <http://groups.google.com/groups?group=comp.lang.java.gui> Q2.4 What is an SSCCE? Short/small, self contained, compilable, example (source code). It would be best if you provide such short (see the group's charter) example source code in your first request for help. When asked for one, please don't complain that your source code is too large, too tricky, too 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. For more information about hacking some example code together, go to <http://www.physci.org/codes/sscce.jsp> Q2.5 Why don't people like top-posting? What is top-posting? See the following Question & Answer (well, 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 Q2.6 Is there more about posting to newsgroups and asking questions? Yes, see e.g. the following for making the most out of a newsgroup (ignore the hacker slang): <http://www.catb.org/~esr/faqs/smart-questions.html> [The author of that web page has requested a notice that he is not a general help desk for all your problems]. <http://www.yoda.arachsys.com/java/newsgroups.html> Also see the newsgroups: <news:news.newusers.questions> <news:news.answers> And RFC 1855. E.g. at <ftp://ftp.rfc-editor.org/in-notes/rfc1855.txt> <http://www.faqs.org/rfcs/rfc1855.html> Q2.7 Does Sun support or endorse the newsgroup? From time to time a poster could be spotted apparently working for Sun's Swing development team and answering questions. But that hasn't happened in a long time. You sometimes see announcements posted to the group, treating the group as as write-only media. But that's it. It is safe to say that the newsgroup isn't of much interest for Sun, and that suggestions or informed opinions posted to the group will most likely not be seen, noted or followed-up by Sun. You might want to try <http://developer.java.sun.com/developer/bugParade/> or <http://jcp.org/> if you want to suggest some changes to Java. Good luck. See also: "Q1.5 Does Sun support or endorse this FAQ?" SECTION 3 - The Top 5 Questions ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Q3.1 My GUI freezes or doesn't update. 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, it is rather simple: public void actionPerformed(ActionEvent e) { new Thread(new Runnable() { public void run() { // // Do some time consuming task // ... // // Update the GUI from within the task // synchronous: invokeAndWait() // asynchronous: invokeLater() // SwingUtilities.invokeLater(new Runnable() { public void run() { // // GUI code // } }); } }).start(); // start the thread } 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> SwingWorker is earmarked for inclusion into Java Standard Edition 1.6. For some special cases you can give paintImmediately() a look. See <http://java.sun.com/products/jfc/tsc/articles/painting/index.html> for some information about using paintImmediately(). See also: "Q3.2 How do I update the GUI from another thread?" Q3.2 How do I update the GUI from another thread? If you have to update the GUI from another thread (e.g. once you offloaded a time consuming task from the EDT to another thread) you should use the javax.swing.SwingUtilities.invokeLater() or javax.swing.SwingUtilities.invokeAndWait(). Often you want invokeLater(). The code is rather simple. E.g. when using an anonymous class: SwingUtilities.invokeLater(new Runnable() { public void run() { // Code to be executed on the EDT } } ); // Current thread will immediately continue here And SwingUtilities.invokeAndWait(new Runnable() { public void run() { // Code to be executed on the EDT } } ); // Current thread will wait until code has been executed on // the EDT. Again, see <http://java.sun.com/products/jfc/tsc/articles/threads/threads1.html> for more details. See also: "Q3.1 My GUI freezes or doesn't update. What to do?" Q3.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. Q3.4 My graphics on a Canvas/JPanel/JComponent, etc. gets corrupted, or I get a null pointer exception when trying to draw. How can I avoid this? Do not use Component.getGraphics(). Instead, subclass and override the paint() (AWT), or paintComponent() (Swing) method. Component.getGraphics() simply can't work. Java's normal drawing mode is called 'passive rendering'. It is based on a callback mechanism. The drawing code is supposed to sit passively there until Java asks it to draw something by calling your paint()/paintComponent() method. At that moment you are supposed to provide the Component with the drawings you would like to do by using the provide Graphics/Graphics2D drawing context. You are not supposed to "push" graphics information into a component using getGraphics() or any other means. This mechanism is necessary so Java can support graphics systems which don't remember a window's contents when it is obscured (e.g. overlayed by another window). When the window becomes visible again, such graphics systems have to ask the application to reconstruct the window content. Therefore, paint()/paintComponent() is supposed to be the memory of a component. getGraphics(), however, doesn't have any recollection of previous drawing operations. So once a drawing done via getGraphics() is lost, it can't be reconstructed. There is nothing in there that stores the old drawing data, and there is nothing in AWT/Swing which informs getGraphics() to do some re-drawing. In addition, there are situations where Component.getGraphics() simply returns null. This is a defined behavior of the method. And finally, most users of getGraphics() forget to dispose the Graphics object after usage, thus creating a resource leak. See <http://java.sun.com/products/jfc/tsc/articles/painting/index.html> for more information about Java's normal painting model. For very special cases (games, animations, slide shows, etc.), you might want to flip to an 'active rendering' mode. Active rendering best works when you wholly own the screen in full-screen exclusive mode. See also: "Q4.4 What is full-screen exclusive mode?" Q3.5 How to create a transparent or non-rectangular window? You can't in a good, platform independent way. Although particular Swing components can be 'transparent', the problem is the top-level window. That window is always rectangular, and in almost all Java implementations non-transparent. Depending on your circumstances, platform, etc., one of the following might (partly) work: 1) Ugly Hack, all platforms, Java 1.3+ 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. <http://www-128.ibm.com/developerworks/java/library/j-iframe/> is a JFrame subclass which uses Robot to add transparency. 2) Windows The nativeskin.jar of the Skin LnF at <https://skinlf.dev.java.net/> provides a Windows-only region feature for building non-rectangular windows. It comes with a native Win32 library, so applications written with this library are not portable to other platforms. It should in principle be possible to write a similar library for other GUI systems (e.g. X11 with the very common shape extension). 3) Apple OS X Apple's Java for OS X observes the alpha-component of the window background color, like one would expect from any Java implementation. Therefore, any amount of transparency of a top-level window can be configured by just constructing an appropriate Color object and setting it as background color. E.g. the following sets a completely transparent background: window.setBackground(new Color(0, 0, 0, 0)); Non rectangular-looking windows can be constructed by - Turning the normal window decoration off (if any), and - Using an image (in a format which supports an alpha component, e.g. PNG) as the window background. 4) Applet An alternative for applets which might be good enough in some cases can be found here: <http://java.sun.com/docs/books/faq/src/app/MatchBackgroundExample.html> See also: "Q6.1.2 How to create a transparent widget?" "Q6.1.3 How to create a non-rectangular widget?" SECTION 4 - Architecture ~~~~~~~~~~~~~~~~~~~~~~~~ Q4.1 What is this Model-View-Controller (MVC) stuff? MVC is a way to structure an application. It is based on the idea of separating the presentation of data from the data itself. MVC originated in the Smalltalk world and has since then become a common design pattern. Swing uses a variant of MVC (not THE original MVC as Smalltalk users will point out). The following TSC article contains a good description of Swing's architecture and MVC variant: <http://java.sun.com/products/jfc/tsc/articles/architecture/index.html> Q4.2 What is the Swing single-threading issue? There is really no issue. With very few exceptions Swing is not thread safe. But it can be safely used in a multi-threaded environment if the necessary precautions are taken. There are three simple things to take care of, not more: 1. Call all Swing API methods from the event dispatching thread (EDT), unless the API documentation states that a method can be called from another thread (is thread safe). NOTE: The API documentation has been known to be wrong in the past. Check Sun's bug parade when in doubt. 2. If you are not in the EDT, but need to call a Swing API method, use either invokeAndWait() or invokeLater() to schedule your code for execution on the EDT. 3. Do not block the EDT. That is, don't run time-consuming tasks on the EDT. Instead, run these tasks in a separate thread and use an invoke...() method to update the GUI from that separate thread. There is really nothing more to it. See also: "Q3.1 My GUI freezes or doesn't update. What to do?" "Q3.2 How do I update the GUI from another thread?" "Q4.3 What is the right way to start a Swing GUI?" Q4.3 What is the right way to start a Swing GUI? Sun almost silently changed the recommended GUI startup procedure. At the beginning of 2004 the examples in Sun's GUI tutorial were changed, and some rather short "explanation" was given. The explanation can be summarized as: "There is a threading bug somewhere in Swing. To work around, already build and start the GUI from the EDT". No more useful information is provided, e.g. if Sun knows the root cause of the bug and intends to fix it. So now the official way to build and start (calling setVisible(true)) the GUI is to use invokeLater(). The shortest version of a GUI application's main() method becomes: public static void main(String[] args) { invokeLater(new Runnable() { public void run() { // // Build and start the GUI here. // } }); } See also: "Q4.2 What is the Swing single-threading issue?" Q4.4 What is full-screen exclusive mode? As the name implies, a means to use the whole screen exclusively for your applications, like it is e.g. needed for games, industrial control applications, or slide shows. It was introduced with java 1.4. You are much closer to the graphics hardware then in normal mode. This requires some programming techniques which are different from normal Swing/AWT programming. Full-screen exclusive mode is often used together with active rendering. See <http://java.sun.com/docs/books/tutorial/extra/fullscreen/index.html> Q4.5 What is active rendering? Active rendering is an a drawing method where you 'push' your graphics on the screen instead of the usual callback-based method ('passive rendering') where you wait until you are asked to draw something. Active rendering results in a completely different application architecture, and is only recommended for games, animations, etc. It works best in full-screen exclusive mode. It is often combined with page-flip BufferStrategy. See also: "Q4.4 What is full-screen exclusive mode?" "Q3.4 My graphics on a Canvas/JPanel/JComponent, etc. gets corrupted, or I get a null pointer exception when trying to draw. How can I avoid this?" PART II ======================================================================== SECTION 5 - Window / [J]Frame / [J]Dialog (Top-Level Containers) ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Q5.1 How can I ensure a window is always on top of all other windows using AWT or Swing? 1) Before Java 1.5 you couldn't at all: AWT and Swing didn't provide this feature. All you could do was to use a (modal) [J]Dialog, and make sure the [J]Dialog is provided with the correct parent/owner in the constructor. 2) Since Java 1.5: You have Window.setAlwaysOnTop(), which is inherited by the other top-level containers like JFrame. However, the implementation is incomplete, and not supported on all platforms. There are especially problems with a number of Unix version / Window manager version combinations. Q5.2 How can I (de)iconify a window? 1) Before Java 1.2 you had to revert to native calls. 2) Since Java 1.2 you can use [J]Frame.setState(). 3) Since Java 1.4 you can use [J]Frame.setExtendedState(), too. setExtendedState() provides more features than setState(). Q5.3 How can I replace/remove the icon in the title bar (window decoration) of a [J]Frame? Use setIconImage(). To revert to the platform's default icon use: frame.setIconImage(null); On some platforms this might remove the icon. Alternatively you can try a transparent Image if you don't want to have an icon. Q5.4 How to replace the icon in the title bar (window decoration) of a [J]Dialog? There is only a partial solution to this problem, and it is not recommended. A dialog gets its icon from its parent frame. You can create a dummy frame which you don't show, set the icon of that dummy frame, and use it in the constructor of the dialog as the dialog's owner: JFrame dummy = new JFrame(); Image icon = ... dummyFrame.setIconImage(icon); JDialog dialog = new JDialog(dummy); However, this is dangerous. Certain GUI behavior depends on a correct [J]Frame (parent window) <-> [J]Dialog (child window) relation. Introducing a dummy parent breaks this relation. Things which can go wrong include (de)iconising of all windows of an application, and ensuring a modal dialog is always placed on-top of the main window. Q5.5 My modal dialog goes behind the main window. How can I ensure it is in front instead? Make sure you have properly set up the 'owner' of the dialog in the dialog's constructor. Don't use null, and don't use a dummy frame (to set the dialog's icon). Q5.6 How to bind the escape key to the JDialog cancel operation? First the bad news. Some JComponent, like JComboBox or JTable use the escape key by themselves. As a result, the following will not work, unless you manage to remove the escape key handling from these components. To bind the escape key to some operation use code like it follows: final String ESC_ACTION_KEY = "ESC_ACTION_KEY"; theDialog.getRootPane().getActionMap().put( ESC_ACTION_KEY, new AbstractAction() { public void actionPerformed(ActionEvent e) { // // perform the action associated with the escape key // // e.g. inform all window listeners, or just call // dispose() // } }); theDialog.getRootPane().getInputMap( JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT ).put( KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0), ESC_ACTION_KEY ); Q5.7 How can I implement my own JFrame/JDialog close handling? Ironically, by setting the JDialog, JFrame, or JInternalFrame default close operation to do nothing: theJDialog.setDefaultCloseOperation(JDialog.DO_NOTHING_ON_CLOSE); And then adding an own window listener: theJDialog.addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent we) { // // Ask for closing confirmation, // perform any necessary cleanup, etc. here. // Close window if desired. // // we.getWindow().dispose(); // close window } }); Q5.8 How Do I center a window on the screen? How do I get the screen size? 1) Manually, pre 1.4: Use java.awt.Toolkit.getDefaultToolkit().getScreenSize() to get the screen size, and do the math: import java.awt.*; Dimension winSize = win.getSize(); Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize(); win.setLocation( (screenSize.width - winSize.width) / 2, (screenSize.height - winSize.height) / 2 ); 2) Since 1.4: Window.setLocationRelativeTo(null); Q5.9 How to ensure a minimum or maximum window size? You can't in a good way under most conditions, and only in a limited way under some conditions. Swing and AWT ignore the minimumSize and maximumSize attributes under most conditions. E.g. if your GUI system allows you to interactively resize a window to the size of 1 x 1 pixels Swing/AWT will happily allow this. 1) General hack and pre Java 1.4: A few ugly hacks are possible to correct the size after the unwanted resizing happened. They are ugly, because the window snaps back, due to the fact that the original resizing is not prevent but just corrected with another resizing. This correction can e.g. be done with an event listener, listening to window component resizing events. Or it can be done by overriding the doLayout() method. In both cases, the actual size can be obtained with getWidth()/getHeight() and, if the size is not acceptable, corrected with setSize() (which triggers the second resizing and makes the window jump). 2) Since Java 1.4: In Java 1.4 JFrame.setDefaultLookAndFeelDecorated() was added. It interacts in an undocumented way with the resizing. When set to true, and when the platform allows a L&F window decoration, and when that L&F window decoration is painted by Java (e.g. the Metal window decoration), then the resizing behavior observes the limits as given by the layout - usually. In Java 1.4 a strange bug could sometimes be observed. When using the platform's default L&F, and not the Metal L&F, then JFrame.setDefaultLookAndFeelDecorated(true) changes only the decoration to the Metal L&F decoration, but the window contents as such is still drawn using the default PLAF. This results in a strange mixture of L&Fs. However, the resize limits are observed by such strange-looking windows. 3) Java 1.5: JFrame.setDefaultLookAndFeelDecorated() still interacts with resizing. And the multi-L&F bug has been kind of fixed. "Fixed" in the sense that when JFrame.setDefaultLookAndFeelDecorated() is set to true, not only the windows decoration is changed to the Metal L&F, but also the window contents. Despite the fact that actually the default L&F should be in force. The conclusion is that a combination of Java 1.5, the Metal L&F, and JFrame.setDefaultLookAndFeelDecorated(true) provides a reasonable resizing behavior. See also: "Q5.10 How to ensure a particular aspect ration of a window?" Q5.10 How to ensure a particular aspect ration of a window? You can't in a good way. You can use similar hacks as the ones for enforcing a minimum or maximum window size. See also: "Q5.9 How to ensure a minimum or maximum window size?" Q5.11 How can I delegate the window placement to the window system or manager? First of all, you of course need to use a window manager or system which is capable of placing the windows by its own. The rest depends on the Java version: 1) Before Java 1.5: You have no control over the behavior. If you don't specify an explicit window position this might be interpreted as the position (0, 0) and the window might be placed there, or it might be interpreted as a request to position the window where it best fits. The same might happen when you explicitly specify (0, 0) as the position. Some systems interpret this as a request to place the window where it best fits, others just position the window at (0, 0). 2) Since Java 1.5: Sun has added a hack for specifying the desired behavior. If the java.awt.Window.locationByPlatform system property is set to true, a window manager can place a window with the origin (0, 0) or no specified origin at will. If the property is false, a window with the origin (0, 0) is indeed positioned at (0, 0). The behavior can also be specified on a per window, per setVisible(true) basis by using Window.locationByPlatform(true) immediately before setVisible(true). Q5.12 I need to take some toolbar (dock, panel) size into account when calculating a window position an/or size. How? The information is not available on all platforms. If you are lucky, Toolkit.getScreenInsets() delivers information about the screen border space occupied by the toolbar (dock, panel, or whatever your window system uses). You can uses this information in your calculations. Insets i = theWindow.getToolkit().getScreenInsets( theWindow.getGraphicsConfiguration() ); Do not assume that the desired size is e.g. always in Insets.bottom. Many graphics systems allow to move the toolbar to the top or a side of the screen, too. Or they allow to have multiple toolbars. Always take all four values into account. In general, for cross-platform compatibility, it is best to not rely on this information at all. If it is essential for your application then at least provide some configuration means to manually specify some screen border space. This way people on systems where the information is not available can still use your application as desired by you. See also: "Q6.1.4 What are Insets?" Q5.13 My window layout is displayed incorrectly. I have to move the window, before the layout is right. What's wrong? It is likely that you have changed the layout after you showed the window. The layout is not redone in such cases, unless you tell the container to do so. It is best to first finish the construction of the window before showing it. If you really can't do this, then 1) for AWT: Call invalidate() followed by validate() on the window. 2) for Swing: Call revalidate() on the JComponent (JPanel) of which you changed the layout. Alternatively one can call pack() after invalidating the window. Sometimes it is recommended to follow the validate()/revalidate() call with a repaint(). This should not be necessary, If you have to do so, it might be a component's opaque handling and or paint()/paintComponent() method is broken. See also: "Q8.1 What is the equivalent of AWT's Canvas in Swing?" "Q8.2 When drawing on a JPanel, the background is garbled." "Q3.4 My graphics on a Canvas/JPanel/JComponent, etc. gets corrupted, or I get a null pointer exception when trying to draw. How can I avoid this?" Q5.14 How can I display a Splash Screen at the start of my Program? In general, if you add a splash screen to your application, be nice to your users and allow it to be turned off via some configuration option. Splash screens can become very annoying. 1) Up and including Java 1.5 Use an (AWT) Window to display some image. Even if your application has a Swing based GUI, you should only use AWT classes to build the splash screen. You will anyhow have to wait until the VM comes up, but you avoid having to wait for the loading of the huge amount of Swing classes if you restrict yourself to AWT. 2) Since 1.6 The VM has a command line option for specifying a splash screen image. That image is displayed even before the VM runs any Java code. It can be an animated GIF. java -splash:splashImage.gif ... Java 1.6+ also has an API for accessing and manipulating the splash screen once the Java program is running. See the java.awt.SplashScreen class. And Java 1.6+ has a corresponding Manifest file keyword for specifying a splash screen image: SplashScreen-Image: splashImage.gif SECTION 6 - [J]Component (Widgets) ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 6.1 General Questions Q6.1.1 How do I position components (widgets) on a window? You add them to [J]Panels and use one or more of the many layout managers that come with Java (one for each [J]Panel). And you leave the exact calculation of the position to the layout managers. Learning the layout managers is essential to AWT and Swing programming. Many people don't buy this and think they can get away without. Later they come to the newsgroup and whine because their GUIs don't work on other platforms, or don't look good when resized. But whining will not change their GUIs or the way AWT/Swing works. You have been warned. See and work through: <http://java.sun.com/books/tutorial/uiswing/layout/index.html> Q6.1.2 How to create a transparent widget? First of all it should be noted that being able to have transparent widgets does not guarantee a transparent window, where the desktop shines through. In fact, in general the non-transparent top-level window is where transparency stops. Since the introduction of the lightweight framework in Java 1.1 it is possible for a Component subclasses (if lightweight) to have transparent parts if properly implemented. Lightweight Swing Components are a special case, since Swing adds a switching mechanism so Swing components can basically be switched to have a transparent or filled background. So there are three different cases: 1) Component (excluding Swing and heavyweight) subclasses All it takes is a subclass of Component with a paint() implementation that only draws the non-transparent parts. If an existing lightweight Component is subclassed to build a transparent component, it should be made sure that the superclass doesn't fill the background with the background color in the paint() method. 2) JComponent (Swing) subclasses The Swing JComponent subclasses can usually be switched between painting all pixels within their boundaries or not. This, purely as a side-effect, effectively allows the programmer to control if the JComponent's background is non-transparent or transparent. This behavior is a side-effect of the opaque contract between JComponents and the Swing repaint manager. setOpaque(false) kindly ask a JComponent not to paint all pixels within its boundaries. For most, but not all, Swing components this results in a transparent background. If opaque is false, their paintComponent() method simply does not fill the background before painting the component. Also, the repaint manager, it it finds a component with isOpaque() == false, walks the component hierarchy to find the component below the current one. Because that one needs to be repainted first, to provide the background. Setting setOpaque(false) doesn't work for all JComponents. E.g. some contain nested component (e.g. JScrollPane) which need to be changed to be transparent, too. Or some PLAFs ignore the opaque attribute and always paint every pixel within their bounds. This behavior is not illegal, it is still covered by the opaque contract. But it means that the desired side-effect (transparency) of setting opaque to false will not work. 3) Heavyweight Components Transparent heavyweight components are not supported in Sun's Java implementation. See also: "Q3.5 How to create a transparent or non-rectangular window?" "Q6.1.3 How to create a non-rectangular widget?" Q6.1.3 How to create a non-rectangular widget? The boundaries of a Component (widget) in Java are always rectangular. Non-rectangular Components can be faked by implementing Components with a transparent background. Java will still treat these components as rectangular objects. E.g. The standard layout managers always use the rectangular boundaries to layout Components. If another behavior is required for transparent Components (e.g. partly overlapping boundaries) custom layout mangers need to be implemented. See also: "Q3.5 How to create a transparent or non-rectangular window?" "Q6.1.2 How to create a transparent widget?" Q6.1.4 What are Insets? 1) Simple answer: Insets is a data type, describing additional space around some type of rectangle space, e.g. a component. A better choice of words would have been border space. 2) More detailed answer: There is no consistent usage of the term or the Insets data type in AWT or Swing. Originally Insets were used in AWT to describe the size of window decorations, like the size of a Frame's title bar, and borders in a GridBagLayout. Over the time Insets popped up in all sorts of places in AWT and Swing. Often there is no exact definition of what an Insets data type describes, especially in relation to the size of an item. For a particular usage of Insets one has to figure out if the Insets describe additional or subtractive space. Roughly, one has to figure out, often by experiment, which of the following holds true for a particular situation: usable size = item size - insets total size = item size or usable size = item size total size = item size + inset The exact relation doesn't matter in the trivial case Insets = {0, 0, 0, 0} Q6.1.5 How do I find a component's top-level container (e.g. the window)? Use SwingUtilities.getRoot() 6.2 JTree Q6.2.1 I changed the data / structure for my JTree, but the display doesn't get updated. What's going on? Most likely you are directly manipulating the TreeNodes, instead of updating the data via the TreeModel. TreeNodes don't have any means to inform the JTree about changes. This is the job of the TreeModel. If you use DefaultTreeModel, all the event notification mechanisms are already implemented. If you use an own implementation of TreeModel, you need to implement the necessary event firing yourself. Don't call repaint() on the JTree. The JTree painting is not broken. Your event notification is. Get your event notification right. Q6.2.2 How do I set a custom icon for a node? If you just want to change the icon for all nodes from the default, get a DefaultCellRenderer, use the set...Icon() methods, and set the renderer to be used by the JTree. If you, however, need different icons for different nodes, then the following three steps are involved. 1. Create your own TreeNode implementation (or subclass an existing TreeNode implementation). The following code fragment shows some suggestions. public class ABCTreeNode implements TreeNode { // // a. Add some means to the TreeNode to identify itself, e.g. // public int getType() { /* return a type id */ } // // b. Or let the TreeNode return a corresponding Icon // (possibly a shared instance): // // public Icon getLeafIcon() { /* return icon */ } // public Icon getOpenIcon() { /* return icon */ } // public Icon getClosedIcon() { /* return icon */ } // // c. Or just rely on the type of your subclass (to be // checked with instanceof). You will have to have an // own subclass for each node which needs a different // icon. // // // d. Or let the TreeNode return some layout info object // which then provides getXXXIcon() methods. // // public TreeNodeInfo getInfo() { /* return info object */ } // ... } 2. Subclass DefaultTreeCellRenderer. Override getTreeCellRendererComponent(): public class ABCTreeCellRenderer extends DefaultTreeCellRenderer { Component getTreeCellRendererComponent( JTree tree, Object value, boolean sel, boolean expanded, boolean leaf, int row, boolean hasFocus) { // // Continue, depending on your ABCTreeNode // implementation, // // // a. If you use a getType() method: // ABCTreeNode node = (ABCTreeNode)value; switch(node.getType()) { case: ... setLeafIcon(...); setOpenIcon(...); setClosedIcon(...); break; } // // b. If you use a getIcon() method: // // ABCTreeNode node = (ABCTreeNode)value; // setLeafIcon(node.getLeafIcon()); // setOpenIcon(node.getOpenIcon()); // setClosedIcon(node.getClosedIcon()); // // // c. If you rely on the type: // // if(value instanceof ABCTreeNode) { // setLeafIcon(...); // setOpenIcon(...); // setClosedIcon(...); // } else if(value instanceof ...) ... // // // d. Use the info object: // // ABCTreeNode node = (ABCTreeNode)value; // ABCTreeNodeInfo info = node.getInfo(); // setLeafIcon(info.getLeafIcon()); // setOpenIcon(info.getOpenIcon()); // setClosedIcon(info.getClosedIcon()); // // // Finish with: // return super.getTreeCellRendererComponent( tree, value, sel, expanded, leaf, row, hasFocus); } ... } 3. Finally, use JTree.setCellRenderer() to set your renderer. Q6.2.3 How do I remove all my nodes from a JTree at once? Just replace the model. Deleting all nodes individually is a waste of time. JTree's API documentation does not indicate if it is permissible to use null as a model, but it is known to work in Sun's reference implementation: tree.setModel(null); If you don't trust his code, create an empty model and use it instead of null. 6.3 Styled Text / JEditorPane / JTextPane Q6.3.1 Can I use RTFEditorKit to read RTF documents created by Word? Well, you can try. The RTFEditorKit is, however, very limited. There is a subtle hint in the RTFEditorKit API documentation. It points out that the Swing team "hops" to improve the class in the future. This hint is there since the first release of the class years ago. The newer your Word is, the less likely it is that the RTFEditorKit can read the RTF. There have been reports about crashes when using RTF generated by the latest Word version. Q6.3.2 I have problems using the Swing HTML parser to parse all kinds of HTML. Is this normal? Unfortunately it is. The Swing HTML parser is the old HotJava parser (Sun's pure Java web browser, once a separate product). It is limited and hasn't been updated for a long time. In principle it can deal with HTML 3.2 and style sheets. In practice it is picky. Use another parser if you have problems, or convert your HTML with HTML Tidy or its Java port JTidy before trying to read it. <http://tidy.sourceforge.net/> <http://jtidy.sourceforge.net/> Q6.3.3 Some of my CSS styles don't work out. Is this normal? Yes, for the same reasons as described in the previous answer. See the class javax.swing.text.html.CSS for a list of the officially supported CSS attributes, and brace yourself for some more deviations. Q6.3.4 Can I use Swing's HTML support to write a web browser? You can, but the resulting browser will suffer from the limitations of Swing's HTML parser and CSS handling. The parser was in fact originally developed for Sun's HotJava web browser. But this was years ago, and HotJava was never a serious competitor in the browser business. To get some ideas have a look at <http://java.sun.com/developer/onlineTraining/GUI/Swing1/shortcourse.html#JFCEditorPane> Q6.3.5 Can I use Swing's HTML support to build an on-line help system or e-book? Sure you can. Or, you could take Sun's JavaHelp, which does exactly this. It uses the Swing HTML components to parse and render help text or other text written in HTML. It also provided for navigation and other common help features. Q6.3.6 If HTML support is really so broken in Java, what is it good for? As long you control the generation of the HTML it is quite usable. E.g. the JavaHelp system uses Swing's HTML parser and display capabilities. If you need to handle real-world HTML from sources not under your control, you better look for some other parser. E.g. <http://htmlparser.sourceforge.net/> gets recommended often. 6.4 [J]TextArea Q6.4.1 I append text to a JTextArea. How to ensure the text area is always scrolled down to the end of the text? textarea.setCaretPosition(textarea.getDocument().getLength()); Q6.4.2 How to use several different fonts (styles, sizes) in one [J]TextArea? You can't. Use a J[Editor|Text]Pane. 6.5 [J]Label / [J]Button Q6.5.1 How can I have multiple Lines in a [J]Label? 1) Label You can't. Use a TextArea instead, and turn editing of the TextArea off. 2) JLabel 2.a) Use HTML markup in the label's Text. E.g.: theJLabel.setText("<html>Line 1<br>Line 2<br>Line 3</html>"); 2.b) Or use a JTextArea. Q6.5.2 I want to have a hyperlink in a [J]Label. How can I do this? You probably don't want to have a label, but a button. Just configure a JButton so it looks like a link. E.g. use HTML to format the button's text, and remove the button's border: b = new JButton("<html><a>http://java.sun.com</a><html>"); b.setBorderPainted(false); Provide the button with an ActionListener to handle the ActionEvent when the button is clicked. Implement whatever is desired in the event handler. E.g. start an external web browser. b.addActionListener(new ActionListener() { actionPerformed(ActionEvent e) { // start web browser } }); Q6.5.3 How do I make a JButton the default button in a JDialog? It is rather counter-intuitive, by telling the root pane of the JDialog about the button: theJDialog.getRootPane().setDefaultButton(theJButton); Some PLAFs might ignore this setting. SECTION 7 - JScrollPane ~~~~~~~~~~~~~~~~~~~~~~~ See also: "Q6.1.2 How to create a transparent widget?" "Q6.4.1 I append text to a JTextArea. How to ensure the text area is always scrolled down to the end of the text?" Q7.1 I added a component (e.g. a JPanel with an image) to a scrollpane, but the scrollpane doesn't show it at the right size / without scrollbars, etc. What's wrong? Ensure that the component you added does return its desired size via getPreferredSize(). E.g. if you have a JPanel holding an image, ensure that the JPanel's getPreferredSize() returns the size of the image. PART III ======================================================================== SECTION 8 - Graphics & Painting ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ See also: "SECTION 3 - The Top 5 Questions" Q8.1 What is the equivalent of AWT's Canvas in Swing? It depends on your purpose, and on the level of compliance you want to have with the Swing opaque handling. Use a) JPanel, if you want to have a "complete" component with a UI delegate which handles opaque settings (if paintComponent() is correctly overridden). b) JComponent, if you intend to always draw every pixel in the area of the component (and break the opaque attribute API contract, don't worry, most likely no one will notice). If you need to have your own special key and mouse processing, you might also want to start with JComponent and create your own UI delegate. c) You could even start higher up in the inheritance chain. java.awt.Component is lightweight since Java 1.1. However, you will not get Swing additions like double-buffering. d) Canvas. No joke. Under very special circumstances (e.g. game programming, accelerated graphics) AWT's Canvas might be the right thing to use, even under Swing. Under normal circumstances, however, this is not the right choice, because it brings the problem of mixing heavyweight and lightweight components. If this is all Greek to you, use JPanel. And remember, if you use JComponent or JPanel, override paintComponent(), not paint(). Q8.2 When drawing on a JPanel, the background is garbled. It is likely that you managed to break the opaque handling of the component. Your JPanel instance promises (via getOpaque()) to draw each and every pixel under its influence, but doesn't. Start your paintComponent() method with a call to super.paintComponent(g) (which honors the opaque attribute and fills the background with the background color). Also, check the setting of the opaque flag, and have a look at the TSC article about "Painting". See the list of resources at the end for a link. Q8.3 How do I generate some charts / plots in Java? If you want to do the drawing in Java, consider using a chart drawing library. E.g. <http://www.jfree.org/jfreechart/index.html> <http://www.jgraph.com/> get recommended often. The web site also has a list of other chart libraries. If you just have to plot some (scientific) data, and if you can live with an external C program, consider using gnuplot <http://www.gnuplot.info/> Millions of scientific papers around the world have been illustrated with gnuplot output. Use Runtime.exec() to pipe the plot commands and data into gnuplot, or just write the data to a file and use gnuplot separately. Q8.4 How to draw some graphs in Java? Non-trivial graph drawing is a serious business. If you want to do it all by yourself, reserve some time and start studying graph drawing algorithms. See the question "I need an algorithm for drawing ..." for information where to start searching for such algorithms. Then, or in parallel, start studying the Java 2D API. If you don't want to concern yourself with the algorithm implementation, use a graph library like: <http://jgrapht.sourceforge.net/> <http://jung.sourceforge.net/> Or check if the algorithm used in Sun's GraphLayout applet demo is suitable for your purpose. The source comes with every SDK, see the directory demo/applets/GraphLayout in you SDK installation. If you can live with an external C tool, give The Dot a try. It is part of graphviz at <http://www.research.att.com/sw/tools/graphviz/refs.html> Or, if you don't require a Java solution and if you are on a Unix with a complete troff installation, including pic, you can use pic for typesetting simple graphs. See also: "Q8.9 I need an algorithm for drawing ..." Q8.5 I want to write a diagram editor. Where to start? Consider using a framework like <http://www.jhotdraw.org/> <http://www.jgraph.com/jgraphpad.html> <http://gef.tigris.org/> for a start. You also might want to familiarize yourself with many of the design patterns in Gamma, E.; Helm, R.; Johnson, R.; Vlissides, J.: Design Patterns: Elements of reusable object-oriented Software. Addison-Wesley professional computing series. Brian W. Kernighan, Consulting Editor. Reading, MA: Addison-Wesley, 1994. And (if you manage to find the old publications), the early work on UniDraw and IDraw by Vlissides is also interesting (in C++). Q8.6 How do I draw lines between JLabels on a JPanel? You are apparently trying to draw a graph by using normal widgets to draw the nodes of your graph. This is not a good idea. Consider using the Java 2D API (nowadays part of J2SE) to draw the complete graph. Have a look at java.awt.geom for predefined shapes. Also check out Sun's 2D Programmer's Guide. Q8.7 How to debug graph painting? The best way is probably to use a debugger and step through the paint[Component]() method. There is also the rather obscure JComponent.setDebugGraphicsOptions() method in Swing. It can be used to turn several kinds of debugging output on or off. Please note that setDebugGraphicsOptions(0) does not turn debugging off. Instead setDebugGraphicsOptions(DebugGraphics.NONE_OPTION) is needed. See also: "SECTION 12 - Resources" Q8.8 I need to draw a tree. How? This seems to be a common homework question, so please have a look at the "Which topics are not welcome in the newsgroup?" to understand why this answer is intentionally vague. If the fixed layout of JTree suits your needs, you could start reading the JTree API documentation. Or you could use a simple (recursive) algorithm. E.g x(node) = K * level(node), and y(node) = M * inorder_rank(node). gives very ugly trees, but trees. If this doesn't get you started, ask your professor or tutor for more hints. Consult your text book about (inorder) tree traversal, and consult <http://home.earthlink.net/~patricia_shanahan/beginner.html> Q8.9 I need an algorithm for drawing ... First of all, it really helps to know a little bit about math and geometry. It also helps to know a little bit about coordinate transformations. Then start with the FAQ for <news:comp.graphics.algorithms> which is at <http://www.magic-software.com/CgaFaq.pdf> Q8.10 When I subclass JPanel/JComponent, I need to override paint(), right? Wrong. You are supposed to override paintComponent(), not paint(). You would override paint() only when subclassing Canvas. Swing has a slightly different painting model than AWT. Have a look at the painting article in TSC. This is highly recommended reading if you plan to do any kind of drawing or develop a custom component. See also: "SECTION 12 - Resources" Q8.11 Why does drawImage() fail when I try to display a loaded image? Why are the width and height of my loaded image both zero? The image has most likely not finished loading. Use a java.awt.MediaTracker to monitor the loading of the image (see the MediaTracker API documentation for details and an example). Alternatively, use javax.swing.ImageIcon to load the image. It already contains the necessary MediaTracker handling. Q8.12 How do I resize (zoom in/out) my Graphics? First of all, it is best to not touch your original data if the resizing is only for display purposes. So, if you just want to resize the graphics for display purposes you have to work on three areas: 1. Use an AffineTransform to construct an appropriate transformation (see its scale() method). You can also use the Graphics2D.scale() convenience method instead. In both cases, however, you should ensure that you reset the Graphics2D object in your paintComponent() method to its original transformation before leaving the method. 2. When you do zooming, it might be handy to not only scale the graphics, but to also resize the JPanel on which you are drawing, so the JScrollPane in which you have placed the JPanel updates its scrollbars accordingly. Be aware, however, that the JPanel size is in device coordinate space, while your geometry data is hopefully in user coordinate spaces. double zf = 1.0; public void setZoom(double zf) { // better do some sanity check on the // zoom factor, too this.zf = zf; theJPanel.setPreferredSize(baseWidth * zf, baseHeight *zf); theJPanel.repaint(); } public void paintComponent(Graphics g) { super.paintComponent(g); // // Save the old AffineTransform state of the // graphics object. Either by remembering the // AffineTransform, or by just working on a // copy of the Graphics object. // // Variant (a): Save the old AffineTransform Graphics2D g2d = (Graphics2D)g; AffineTransform orig = g2d.getTransform(); // // Variant (b): use a copy of the Graphics context // Graphics2D g2d = (Graphics2D) ((Graphics2D)g).create(); // // Now scale the Graphics context // g2d.scale(zf, zf); // ~~~ Draw graphics here ~~~ // // Reset Graphics state before leaving // Variant (a): If old AffineTransform has been saved: // Reset transformation g2d.setTransform(orig); // // Variant (b): If a copy of the graphics was used: // Just destroy copy // g2d.dispose(); } 3. If you provide some means to select parts of the graphics or interact in some other way with the graphics using the mouse, you have to scale (divide by the zoom factor) the mouse event coordinates, too. You could also use an AffineTransform with the inverse scaling for this, but for the simple linear scaling used for zooming, dividing the coordinates will do. The easiest is probably to do this calculation in the event handler which is supposed to provide the interaction with the graphics. Sometimes it is suggested to use the glass pane to intercept all mouse events of the JPanel and transform them before forwarding. Q8.13 Where do I find the icons which are used by Swing itself? Most of them are hard-coded, and a lot of them don't have any documented public API and are hidden in the PLAF's implementation. Some can be found in javax.swing.plaf.basic.BasicIconFactory, the ones for Metal can be found in javax.swing.plaf.metal.MetalIconFactory. In general, most PLAFs list their icons in the UIDefaults. See also: "Q10.4 How do I get all the UIDefaults, and what do they mean?" Q8.14 Where do I find typical application icons? See also: "12.3 Icons" SECTION 9 - Fonts ~~~~~~~~~~~~~~~~~ See also: "Q6.4.2 How to use several different fonts (styles, sizes) in one [J]TextArea?" "Q3.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?" "Q10.3 How do I change a color/font/etc. globally for an application?" "Q10.4 How do I get all the UIDefaults, and what do they mean?" Q9.1 Which Fonts can I use? Can I use font <xyz>? See the API documentation of the Font class for a start: <http://java.sun.com/j2se/1.5.0/docs/api/java/awt/Font.html> Q9.2 How to turn on text antialiasing in Swing? First of all, people have reported mixed results with using antialiasing in Swing. The improvement is sometimes not as expected, depending on the font, font size (it works better with larger fonts), font renderer in use, and e.g. monitor resolution. So it is best to test the results on the target platform, and make it a configurable option. 1) Before Java 1.5: Turning text antialiasing on for one particular component is relatively simple, turning it on for all components of a GUI is a nightmare. To turn antialiasing on for a component you have to subclass and override paintComponent(): public void paintComponent(Graphics g) { Graphics2D g2 = (Graphics2D) g; g2.setRenderingHint( RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON ); g2.setRenderingHint( RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY ); super.paintComponent(g2); } } To turn antialiasing on for a whole GUI you would have to subclass all used components the way described above, and then only use this components. Or use a PLAF which does this for you. Such PLAFs are e.g. <http://smoothmetal.sourceforge.net/> <http://wraplf.l2fprod.com/> 2) Since Java 1.5 It is now possible to globally turn on text antialiasing by setting the system property "swing.aatext" to "true" on the command line. The pre Java 1.5 methods still work, too. Q9.3 Why do some of my characters get displayed as squares? 1) If this happens in a GUI Java didn't find matching a visual representation (glyph) in the font you used. In such a case Java uses a square to denote that it doesn't know how to draw the character. The fix depends on what you intended to do: 1a) If you indeed intended to display the character in the GUI, then you need to find and use a font which contains the glyph for the character. <http://java.sun.com/j2se/1.5.0/docs/guide/intl/font.html> has some information which glyphs can be found in Java 1.5's bundled physical fonts. The documents at <http://java.sun.com/j2se/1.4.2/docs/guide/intl/fontprop.html> <http://java.sun.com/j2se/1.5.0/docs/guide/intl/fontconfig.html> describes how Java's font lookup happens, and how this can be changed on VM level. However, using java.awt.createFont() is much more preferable than requiring an end-user to change the VM configuration. 1b) If you didn't intend to display the character, then you probably felt victim to the fact that many Java widgets don't interpret control characters like new-lines in Strings. For example JLabel or JButton do not do this. Instead, they just try to render the character with a glyph. But usually a font doesn't contain glyphs for control character code points. So you get squares instead. If you need to get control characters interpreted, then in AWT you would need to implement an own widget (based on a Canvas), which lays-out the text as desired. In Swing you can use HTML formating for most widgets: // Does not work: JLabel jl = new JLabel("Line1\nLine2"); // Use this instead: JLabel jl = new JLabel("<html>Line1<br>Line2</html>"); If you don't want the characters at all, you have to remove them from the String. 2) You tried to print to the text console This is not a GUI related issue. Most likely the console works with a character set which doesn't contain that character at all. SECTION 10 - Other Common Questions ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Q10.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 to. Q10.2 Can I use Swing for Applets? You can, if you know that the users of your applets have a Java version installed which supports Swing. Swing was first shipped with Java 1.2 (it was available earlier as a separate download). Java 1.2 is an old Java version. You will most probably not develop a Swing application with it. As a consequence, it is likely that your users need browsers which support even later Java versions and match your development version. Sun provides the Java plug-in in the JRE to update the legacy Java versions of some browsers to a current Java version. Your applet users might have to download and install the JRE with the plug-in separately: Programmers: <http://java.sun.com/products/plugin/index.jsp> Consumers: <http://www.java.com/> Alternatively, you should consider to provide your Swing program as an application, not an applet, and use Java Web Start to deploy it: <http://java.sun.com/products/javawebstart/> JavaWebStart is nowadays also a part of the JRE. Q10.3 How do I change a color/font/etc. globally for an application? 1) In case you are using Metal, the best way is to implement an own javax.swing.plaf.metal.MetalTheme, and set it via javax.swing.plaf.metal.MetalLookAndFeel.setCurrentTheme(). 2) For other PLAFs, there is no common API. You can either try to figure out some undocumented feature, or use the also undocumented UIDefaults, from which most of the common PLAFs take definitions for colors, fonts, etc. See also: "Q10.4 How do I get all the UIDefaults, and what do they mean?" Q10.4 How do I get all the UIDefaults, and what do they mean? The UIDefaults contain all kinds of undocumented default settings for the current PLAF, like colors, icons, fonts, etc. One can guess the meaning of most of them from the key name, but the keys change for different PLAFs and VM implementations. UIManager.getDefaults() returns the UIDefaults. The returned data type implements, among other things, the Map interface. One can iterate over this map and display all key/value pairs. There are several small programs out there doing exactly this, e.g. a popular one is ShowUIDefaults.java Q10.5 Why is Swing so slow? Swing is slow in the hands of unexperienced GUI programmers. In fact, it has become a common excuse among these to blame Swing for their own shortcomings. Shortcomings which for example include a lack of understanding of the Swing event model or of the repainting mechanism. Abusing a layout manager is also very popular. See also: "Q3.1 My GUI freezes or doesn't update. What to do?" "Q3.2 How do I update the GUI from another thread?" "Q3.4 My graphics on a Canvas/JPanel/JComponent, etc. gets corrupted, or I get a null pointer exception when trying to draw. How can I avoid this?" "Q4.2 What is the Swing single-threading issue?" SECTION 11 - Non-GUI Questions ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Q11.1 How do I do a text/console UI in Java? Java is not well-suited for non-graphical user interfaces. You need platform specific libraries for any sophisticated text UI. The most common ones are based on the Unix curses/ncurses library, like JCurses: <http://sourceforge.net/projects/javacurses/> Note: there happen to be several different libraries out there by the name JCurses or jcurses which are not entirely compatible. Another possibility is charva, a text-base UI toolkit on Linux mimicking the Swing API: <http://www.pitman.co.za/projects/charva/index.html> Q11.2 How can I do this JavaScript thing on my web site? Java is not JavaScript. Try <news:comp.lang.javascript> or another suitable JavaScript newsgroup. Q11.3 I want to make ... First, programmers usually don't "make" things, they program things :-) Precise language helps when you try to communicate with other programmers. The first step to program something is to learn a programming language like Java. Then you need some knowledge about algorithms and data structures. For Java you also throw in some object-oriented concepts and principles. But that's not all. There are many more things to learn, and you can't rush things. See <http://www.norvig.com/21-days.html> Since this is the comp.lang.java.gui FAQ, you should also know that it takes specific skills to develop good or excellent graphical user interfaces. GUI programming is not just about stirring some widgets together. It is about good taste, a devotion to details, some knowledge about color choices, font styles, human-computer-interaction (HCI), style guides, etc. pp. Especially, Java GUI programming is not HTML "programming". In short, if you are in a rush to "make" something, if you can't be bothered to learn the basics, consider something simpler than Java and its GUI toolkits. And no, whining doesn't help. SECTION 12 - Resources ~~~~~~~~~~~~~~~~~~~~~~ There are many good on-line resources regarding Java GUI programming out there. The following list is limited by intention to resources which get recommended often on c.l.j.g, including resources from regulars. 12.1 Sun's Java Web Site * Sun's Swing learning by example tutorial (previously the Example Tutorial, and before that the Quick-Start Tutorial): <http://java.sun.com/docs/books/tutorial/uiswing/learn/index.html> * Sun's complete Swing Tutorial: <http://java.sun.com/docs/books/tutorial/uiswing/index.html> * The courses at <http://java.sun.com/developer/onlineTraining/GUI/> * Sun's Swing Connection (TSC) (Swing developer's site): <http://java.sun.com/products/jfc/tsc/index.html> With the TSC article index (Swing architecture, Swing and threads, painting architecture, etc.): <http://java.sun.com/products/jfc/tsc/articles/> 12.2 Other Sun Sites There are some GUI articles (some of them dated) at: <http://java.sun.com/developer/technicalArticles/GUI/> 12.3 Icons It is suggested to check the licenses of each icon collection before using them in any products. Sun's well hidden set of Icons for Swing's Metal LnF: <http://java.sun.com/developer/techDocs/hi/repository/> More icons: <http://sourceforge.net/projects/icon-collection/> (the link to javalobby.org on that page is broken) The Ximian Open-Office icons (very nice): <http://www.novell.com/coolsolutions/nnlsmag/assets/ooo-stock.zip> Gnome Icons: <http://art.gnome.org/themes/icon/> SVG BlueSphere Icon Theme: <http://svgicons.sourceforge.net/> KDE Icons: <http://www.buzzard.me.uk/jonathan/kde-icons.html> <http://www.kde-look.org/> 12.4 Miscellaneous Examples, Tips and Tricks A collection of examples for doing nice things with Swing components (some are a little bit outdated): <http://www.physci.org/codes/tame/> Originally from <http://www2.gol.com/users/tame/swing/examples/SwingExamples.html> which is now off-line. 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://schmidt.devlib.org/java/> Karsten Lentsch's company web page: <http://www.jgoodies.com/> 12.5 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> 12.6 SDK Documentation GUI Information in the SDK documentation is commonly overlooked, but worth some reading: See your local SDK installation, or Abstract Window Toolkit (AWT) <http://java.sun.com/j2se/1.5.0/docs/guide/awt/index.html> Swing <http://java.sun.com/j2se/1.5.0/docs/guide/swing/index.html> 2D Graphics and Imaging <http://java.sun.com/j2se/1.5.0/docs/guide/2d/index.html> Image I/O <http://java.sun.com/j2se/1.5.0/docs/guide/imageio/index.html> Print Service <http://java.sun.com/j2se/1.5.0/docs/guide/jps/index.html> Input Method Framework <http://java.sun.com/j2se/1.5.0/docs/guide/imf/index.html> Accessibility <http://java.sun.com/j2se/1.5.0/docs/guide/access/index.html> Drag-and-Drop data <http://java.sun.com/j2se/1.5.0/docs/guide/dragndrop/index.html> Swing Examples in the SDK: See the directory demo/jfc in your Java SDK installation. 12.7 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> 12.8 Online Magazines JavaWorld has regular GUI articles. The magazine closed shop on 2004-01-02, but was re-launched March 2004. <http://www.javaworld.com/> 12.9 Java 2D API The Programmer's Guide <http://java.sun.com/j2se/1.5.0/docs/guide/2d/spec/j2d-bookTOC.html> See also the other SDK documentation. The 2D Tutorial (a light introduction, avoids the tough stuff): <http://java.sun.com/docs/books/tutorial/2d/index.html> Sample code See the demo/jfc directory of your SDK installation. Same as: <http://java.sun.com/products/java-media/2D/samples/suite/index.html> 12.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> 12.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> 12.12 More? Q12.12.1 But I need more! Look at the source code. Every SDK comes with the source code of the public parts of the API. The source code is packed in a file called src.zip or src.jar (older SDKs), usually right in the top level directory of your SDK installation. Also, learn how to use a search engine like <http://www.google.com> SECTION 13 - Improvement Suggestions ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Please mail suggestions, corrections, updates, etc. to the author "cljg_faq" at the host "gmx.de". Your "Subject:" line must contain the string [cljg] somewhere in the line, including the square brackets. Otherwise your mail will be discarded automatically. In addition, the address is heavily spam-protected. So if you mail from a spam-invested network, there is little chance to reach the author. Your alternative is to post to c.l.j.g. If you suggest a new entry, please also provide the answer, not only the question. SECTION 14 - Acknowledgments ~~~~~~~~~~~~~~~~~~~~~~~~~~~~ This FAQ contains contributions and help from: Andrew Thompson, David Postill, Manish Hatwalne, Hiwa, Jeanette. # # #