Solving Common Component Problems

 <  Day Day Up  >  

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

Problem: I'm having trouble implementing a model (or some other code that's similar to something already in Java 2 Platform, Standard Edition).

Solution: Look at the J2SE source code. It's distributed with the J2SE SDK, and it's a great resource for finding code examples of implementing models, firing events, and the like.

Problem: Whenever the text in my text field updates, the text field's size changes.

Solution: You should specify the preferred width of the text field by specifying the number of columns it should have room to display. To do this, you can use either an int argument to the JTextField constructor or the setColumns method.

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

Solution 1: If you set the content pane, make sure it's opaque . You can do this by invoking setOpaque(true) on your content pane. Note that although JPanel s are opaque in most look and feels, that's not true in the GTK+ look and feel, which was introduced in 1.4.2. See Adding Components to the Content Pane (page 48) in Chapter 3 for details.

Solution 2: If one or more of your components perform custom painting, make sure you implemented it correctly. See Solving Common Painting Problems (page 740) for help.

Solution 3: You might have a thread safety problem. See the next problem.

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

Solution: Make sure your code is thread-safe. See How to Use Threads (page 632) in Chapter 9 for details.

Problem: My modal dialog gets lost behind other windows .

Solution: If the dialog has a null parent component, try setting it to a valid frame or component when you create it. See bug #4255200 for a possible workaround online at: http://developer.java.sun.com/developer/bugParade/ bugs /4255200.html.

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

Solution 1: Some Swing releases contain bugs in the implementations for the VERTICAL_SCROLLBAR_AS_NEEDED and the HORIZONTAL_SCROLLBAR_AS_NEEDED policies. If feasible for your project, use the most recent release of Swing.

Solution 2: 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.

Solution 3: Make sure you specified the policy you intended for the orientation you intended.

Problem: My scroll pane has no scroll bars.

Solution 1: If you want a scroll bar to appear all the time, specify either VERTICAL_SCROLLBAR_ALWAYS or HORIZONTAL_SCROLLBAR_ALWAYS for the scroll bar policy as appropriate.

Solution 2: 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 the 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. For more information, refer to Sizing a Scroll Pane (page 336) in Chapter 7.

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

Solution: You need to set the minimum size of at least one of the components in the split pane. Refer to Positioning the Divider and Restricting Its Range (page 371) in Chapter 7 for information.

Problem: The setDividerLocation method of JSplitPane doesn't work.

Solution: The setDividerLocation(double) method has no effect if the split pane has no size (typically true if it isn't onscreen yet). You can either use setDividerLocation(int) or specify the preferred sizes of the split pane's contained components and the split pane's resize weight instead. Refer to Positioning the Divider and Restricting Its Range (page 371) in Chapter 7 for information.

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

Solution: If you nest split panes, the borders accumulate ”the border of the inner split pane displays 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 online at: http://developer.java.sun.com/developer/bugParade/bugs/4131528.html.

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

Solution: Try reducing the margin for the buttons. For example:

 button.setMargin(new Insets(0,0,0,0)); 

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.

Solution: This can happen if you use an int instead of an Integer when adding components to a layered pane. To see what happens, make the following change to LayeredPaneDemo :

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.

Solution: 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 can't make HTML tags work in my labels, buttons, etc.

Solution: Make sure your program is running in a release that supports HTML text in the desired component. See Table 1.

Table 1. Releases That Support HTML in Specific Components

Java 2 Release

Corresponding JFC 1.1 Release

Status of HTML support

J2SE v1.2, v1.2.1

JFC 1.1 (with Swing 1.1)

HTML supported in styled text components only.

J2SE v1.2.2

JFC 1.1 (with Swing 1.1.1)

HTML support added for JButton , JLabel , JMenuItem , JMenu , JCheckBoxMenuItem , JRadioButtonMenuItem , JTabbedPane , and JToolTip . Because table cells and tree nodes use labels to render strings, tables and trees automatically support HTML as well.

J2SE v1.3

None

HTML support added for JToggleButton , JCheckBox , and JRadioButton .

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! See Using HTML in Swing Components (page 43) in Chapter 3 for further details.

 <  Day Day Up  >  


JFC Swing Tutorial, The. A Guide to Constructing GUIs
The JFC Swing Tutorial: A Guide to Constructing GUIs (2nd Edition)
ISBN: 0201914670
EAN: 2147483647
Year: 2004
Pages: 171

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net