![]() ![]() ![]() ![]() |
Using Swing Components |
This section discusses problems that you might encounter while using components. If you don't find your problem in this section, consult the following sections:[PENDING: should getting started have a problems section?]
- Solving Common Problems Using Other Swing Features
- Solving Common Layout Problems
- Solving Common Event-Handling Problems
- Solving Common Painting Problems
- Solving Common Conversion Problems
Problem: Certain areas of the content pane look weird when they're repainted.
- If you set the content pane, make sure it's opaque.
JPanel
andJDesktopPane
make good content panes because they're opaque by default. See Adding Components to the Content Pane for details.- If one or more of your components performs custom painting, make sure you implemented it correctly. See Solving Common Painting Problems
for help.
- You might have a thread safety problem. See the next entry.
Problem: My program is exhibiting weird symptoms that sometimes seem to be related to timing.
- Make sure your code is thread-safe. See Threads and Swing
for details.
Problem: The scroll bar policies don't seem to be working as advertised.
Problem: My scroll pane has no scroll bars.
- Some Swing releases contain bugs in the implementations for the
AS_NEEDED
policies. If feasible for your project, use the most recent release of Swing.- If the scroll pane's client can change size dynamically, the program should set the client's preferred size and then call
revalidate
on the client.- Make sure you specified the policy you intended for the orientation you intended.
Problem: The divider in my split pane won't move!
- If you want scroll bars to appear all the time, change the scroll bar policy to ALWAYS.
- If you want the scroll bars to appear AS_NEEDED and you want to force the scroll bars to be needed when the scroll pane is created, you have two choices: either set the preferred size of scroll pane or its container, or implement a scroll-savvy class and return a value smaller than the component's standard preferred size from the
getPreferredScrollableViewportSize
method. Refer to Sizing a Scroll Pane for information.Problem: The
- You need to set the minimum size of at least one of the components in the split pane. Refer to Positioning and Restricting the Divider for information.
setDividerLocation
method doesn't work.Problem: The borders are all messed up in my nested split panes.
- [PENDING: note the bug, there also appears to be a bug with the double version of the setDividerLocation method]
Problem: The buttons in my tool bar are too big.
- [PENDING: talk about the bug]
Problem: The components in my layered pane aren't layered correctly. In fact, the layers seem to be inversed--the lower the depth the higher the component.
- Try reducing the margin for the buttons. For example:
button.setMargin(new Insets(0,0,0,0));
- This can happen if you use an
int
instead of anInteger
when adding components to a layered pane. To see what happens, make the following change toLayeredPaneDemo
:
Change this... to this... layeredPane.add(label, new Integer(i));
layeredPane.add(label, i);
Problem: The method call
colorChooser.setPreviewPanel(null)
does not remove the color chooser's preview panel as expected.
- A
null
argument specifies the default preview panel. To remove the preview panel, specify a standard panel with no size, like this:colorChooser.setPreviewPanel(new JPanel());
Problem: I'm trying to handle certain events from a component, but the component isn't generating the events it should.
- First, make sure you registered the right kind of listener to detect the events. See whether another kind of listener might detect the kind of events you need.
- Make sure you registered the listener on the right object.
- Did you implement the event handler correctly? For example, if you extended an adapter class, then make sure you used the right method signautre. Make sure each event-handling method is
public void
, that the name spelled right and that the argument is of the right type.- If you still think that the component isn't generating the events it should, check the Java Developer Connection to see whether this is a known bug.
Problem: The document for an editor pane (or text pane) isn't firing document events.
- [PENDING: wordsmith to fit] The document and editor kit might change when loading text from a URL. For example, if you load an editor pane or text pane with HTML that was previously loaded with plain text, the document will change to an
HTMLDocument
instance and the editor kit will change to anHTMLEditorKit
instance. If your program dynamically loads text into an editor pane or text pane, make sure the code adjusts for possible changes to the document and editor kit (re-register document listeres on the new document, and so on).- If you load an editor pane from a URL and the format of the text found at the URL is of a known type, the editor pane's editor kit instance and document instance change dynamically. So, if you load a particular editor pane or text pane from a URL, do not also set its editor kit with
setEditorKit
or its document withsetDocument
. They might get changed out from under you and cause hard-to-find bugs. [PENDING: make this a common problem]
![]() ![]() ![]() ![]() |
Using Swing Components |