User Interface Problems

Swing Components

This section discusses problems that you might encounter while using Swing components.

Problem: I can't make HTML tags work in my labels, buttons, and so on.

Problem: Certain areas of the content pane look weird when they're repainted.

Problem: My program is exhibiting weird symptoms that sometimes seem to be related to timing.

Problem: The scroll bar policies don't seem to be working as advertised.

Problem: My scroll pane has no scroll bars.

Problem: The divider in my split pane won't move!

Problem: The setDividerLocation method doesn't work.

Problem: The borders on nested split panes look too wide.

Problem: The buttons in my tool bar are too big.

Problem: The components in my layered pane aren't layered correctly. In fact, the layers seem to be invertedthe lower the depth, the higher the component.

Problem: The method call colorChooser.setPreviewPanel(null) does not remove the color chooser's preview panel as expected.

Layout

Problem: How do I specify a component's exact size?

If you specify new size hints for a component that's already visible, you then need to invoke the revalidate method on it, to make sure that its containment hierarchy is laid out again. Then invoke the repaint method.

Note

No matter how you specify your component's size, be sure that your component's container uses a layout manager that respects the requested size of the component. The FlowLayout and GridBagLayout managers use the component's preferred size (the latter depending on the constraints that you set), but BorderLayout and GridLayout usually don't. The BoxLayout manager generally uses a component's preferred size (although components can be larger) and is the only layout manager that respects the component's maximum size.

Problem: My custom component is being sized too small.

Event Handling

This section discusses problems that you might encounter while handling events.

Problem: I'm trying to handle certain events from a component, but the component isn't generating the events it should.

Problem: My combo box isn't generating low-level events, such as focus events.

Problem: The document for an editor pane (or text pane) isn't firing document events.

Swing Graphics

Problem: I don't know where to put my painting code.

Problem: The stuff I paint does not show up.

Problem: The background of my applet shows up, but the stuff in the foreground does not show up.

Problem: My component's foreground shows up, but its background is invisible. The result is that one or more components directly behind my component are unexpectedly visible.

Problem: I used setBackground to set my component's background color, but it seemed to have no effect.

Problem: I'm using the exact same code as a tutorial example, but it doesn't work. Why?

Problem: How do I paint thick lines? Patterns?

Swing Conversion

For information on converting your programs to Swing, see the book The JFC/Swing Tutorial or the online tutorial at http://java/sun/com/docs/books/tutorial/uiswing/converting.

Problem: I'm seeing weird problems that are either intermittent or dependent on timing.

Problem: My applet/dialog/frame is blank.

Problem: In the Swing version of my program, the list/text component is missing its scroll bars.

Problem: Although I'm using the same grid bag layout code as before, one scrollable component is tiny.

Problem: I'm not getting the kinds of events I expected for the Swing component I'm using.

Категории