|Using Components, the GUI Building Blocks|
Problem: How do I increase or decrease the number of components in a container?
Problem: My window never shows up!
- To add a component to a container, use one of the three forms of the Container
add()method. See General Rules for Using Components for information. To remove a component from a container, use the Container
removeAll()method. Or just add the component to another container, since that automatically removes the component from its previous container.
Problem: My component never shows up!
- Did you either set the size of the window or pack it?
- Did you make the window visible using the
Problem: My custom component doesn't get updated when it should.
- Did you add the component to its container using the right
add()method for the container's layout manager? BorderLayout (the default layout manager for windows), silently fails to add your component if you call the one-argument version of
add(). See the Laying Out Components within a Container lesson for examples of using the
- If you're not using the default layout manager, did you successfully create an instance of the right layout manager and call
setLayout()on the container?
- If the component is added properly, but to a container that might already be visible, did you call
validate()on the container after adding the component?
- If your component is a custom component (a Canvas subclass, for example), does it implement the
preferredSize()methods so that they return the correct size of the component?
- If you use a non-AWT layout manager, or none at all, does the component have a reasonable size and display coordinates? In particular, if you use absolute positioning (no layout manager), you must explicitly set the size of your components, or they just won't show up. See Doing Without a Layout Manager .
Problem: My component isn't getting XYZ event.
- Make sure that the component's
repaint()method is called every time the component's appearance should change. For standard components, this isn't a problem, since platform-specific code takes care of all the drawing of the component. For custom components, however, you have to explicitly call the
repaint()method on the component whenever the component's appearance should change. Invoking
repaint()on a container of the component is not good enough. See How to Use Canvases for more information.
Problem: My application doesn't get a WINDOW_DESTROY event, so I can't close my window (or quit the application or whatever)!
- Check whether the component is getting any events, at all. If it isn't, make sure you're referring to the right component -- that you instantiated the right class, for example.
- Make sure your component passes through the kind of event you're trying to catch. For example, many standard components don't pass through mouse events, so the AWT can't generate Event objects for them. Can you use another event type, such as ACTION_EVENT (handled by the
action()method), instead? If not, you might be forced to implement a Canvas subclass (or a Panel or Applet subclass) so that you can see all the events that occur.
Problem: All your examples are of applets. How do I apply them to applications?
- In a Frame subclass, implement
handleEvent()so that it reacts to the the WINDOW_DESTROY event. You can't catch WINDOW_DESTROY events in a Panel subclass, for example, since the Frame is above it in the component hierarchy. To quit, use
System.exit(). To destroy the window, you can call
dispose()or you can just
hide()the Frame and, if you're not going to use it again, make sure that all references to it are set to null.
Problem: Whenever I execute a Java application with a GUI, I get this annoying error message:
- Except where noted, anywhere in this trail that you see a subclass of the Applet class, you can substitute a subclass of the Panel class or, if the subclass isn't used as a container, a subclass of the Canvas class. In general, it's easy to convert an applet into an application, as long as the applet doesn't rely on any special applet abilities (such as using methods defined in the Applet class).
To convert an applet into an application, you need to add a
main()method that creates an instance of a Frame subclass, creates an instance of the Applet (or Panel or Canvas) subclass, adds the instance to the Frame, and then calls the
start()methods of the instance. The Frame subclass should have a
handleEvent()implementation that handles WINDOW_DESTROY events in the appropriate way.Warning: Cannot allocate colormap entry for default background
If you don't see your problem in this list, see Common Layout Problems and, for custom components, Common Graphics Problems. You might get some insight into your problem by reading Details of the Component Architecture.
- This message occurs only on Motif systems. It occurs when the Motif library is initialized and finds that there's no room in the default colormap to allocate its GUI colors. The solution is to run fewer "colormap hogging" applications on your desktop. The Java runtime adapts itself to whatever colors are in the default palette, but the Motif library is less forgiving.
|Using Components, the GUI Building Blocks|