Start of Tutorial > Start of Trail > Start of Lesson | Search |
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:
- Solving Common Problems Using Other Swing Features
- Solving Common Layout Problems
- Solving Common Event-Handling Problems
- Solving Common Graphics Problems
- Solving Common Conversion Problems
Problem: I can't make HTML tags work in my labels or buttons or... (See How to Use JButton Features for an example.)
- Make sure your program is running in a release that supports HTML text in the desired component. The following table shows which releases support HTML in which components.
API Version Corresponding JFC 1.1 Release Corresponding Java 2 Release Status of HTML support Swing 1.1 JFC 1.1 (with Swing 1.1) Java 2 v 1.2, Java 2 v 1.2.1 HTML supported in styled text components only. Swing 1.1.1 Beta 1 JFC 1.1 (with Swing 1.1.1 Beta 1) none HTML support added for JButton
andJLabel
. Because table cells and tree nodes use labels to render strings, tables and trees automatically support HTML, as well.Swing 1.1.1 Beta 2 JFC 1.1 (with Swing 1.1.1 Beta 2) none HTML support added for JMenuItem
,JMenu
,JCheckBoxMenuItem
,JRadioButtonMenuItem
,JTabbedPane
, andJToolTip
.Swing 1.1.1 (expected) JFC 1.1 (with Swing 1.1.1) (expected) Java 2 v 1.2.2 (expected) Same as Swing 1.1.1 Beta 2.
JCheckBox
andJRadioButton
don't support HTML yet. We don't know yet when that support will be added.- If you can't guarantee that your program will be executed only with a release that supports HTML text in the desired component, don't use that feature!
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 Graphics 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
VERTICAL_SCROLLBAR_AS_NEEDED
and theHORIZONTAL_SCROLLBAR_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 a scroll bar to appear all the time, specify either
VERTICAL_SCROLLBAR_ALWAYS
orHORIZONTAL_SCROLLBAR_ALWAYS
for the scroll bar policy as appropriate.- 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 on nested split panes look too wide.
- In some releases of Swing, there is a bug whereby a call to
setDividerLocation
doesn't work unless the split pane is already on screen. For information and possible workarounds, see bug #4101306 and bug #4182558 in the Bug Parade at the Java Developer's Connection.Problem: The buttons in my tool bar are too big.
- If you nest split panes, the borders accumulate--the border of the inner split panes display next to the border of the outer split pane causing borders that look extra wide. The problem is particularly noticeable when nesting many split panes. The workaround is to set the border to null on any split pane that is placed within another split pane. For information, see bug #4131528 in the Bug Parade at the Java Developer's Connection.
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());
Start of Tutorial > Start of Trail > Start of Lesson | Search |