| Using Components, the GUI Building Blocks

## Common Component Problems (and Their Solutions)

> Problem: How do I increase or decrease the number of components in a container?
> - 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 `remove()` or `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 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 `add()` method.
> - 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 `minimumSize()` and `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 custom component doesn't get updated when it should.
> - 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 component isn't getting XYZ event.
> - 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: My application doesn't get a WINDOW_DESTROY event, so I can't close my window (or quit the application or whatever)!
> - 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: All your examples are of applets. How do I apply them to applications?
> - 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 `init()` and `start()` methods of the instance. The Frame subclass should have a `handleEvent()` implementation that handles WINDOW_DESTROY events in the appropriate way.

>   See AnimatorApplet.java and AnimatorApplication.java for examples of an applet and an application that implement the same functionality.

> Problem: Whenever I execute a Java application with a GUI, I get this annoying error message:

> >     Warning:
> >         Cannot allocate colormap entry for default background

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

| Using Components, the GUI Building Blocks