Solving Common Problems Using Other Swing Features

 <  Day Day Up  >  

Problem: My application isn't showing the look and feel that I have requested via UIManager.setLookAndFeel .

Solution: You probably either set an invalid look and feel or set your look and feel after the UI manager loaded the default. If you're sure that your look and feel is valid and setting it is the first thing your program does (at the top of its main method, for example), check whether you have a static field that references a Swing class. This reference can cause the default look and feel to be loaded if none has been specified. For more information, including how to set a look and feel after the GUI has been created, see the How to Set the Look and Feel (page 628) in Chapter 9.

Problem: Why isn't my component getting the focus?

Solution 1: Is it a custom component (for example, a direct subclass of JComponent ) that you created? If so, you may need to give it an input map and mouse listener. See Making a Custom Component Focusable (page 592) in Chapter 9 for more information and a demo.

Solution 2: Is the component inside of JWindow ? The focus system requires a JWindow 's owning frame to be visible for any components in the JWindow to get the focus. By default, if you don't specify an owning frame for a JWindow , an invisible one is created. The solution is either to specify a visible owning frame when creating the JWindow or to use a JDialog or JFrame instead.

Problem: Why can't my dialog receive the event generated when the user presses the Escape key? This worked until I ported to release 1.4.

Solution 1: If your dialog contains a text field, it may be consuming the event. (Prior to release 1.4.0, the text field didn't get the focus.)

Solution 2: If you want to get the Escape event regardless of whether a component consumes it, you should use a KeyEventDispatcher .

Solution 3: If you want to get the Escape event only if a component hasn't consumed it, then register a key binding on any JComponent in the JDialog , using the WHEN_IN_FOCUSED_WINDOW input map. For more information, see How to Use Key Bindings (page 623) in Chapter 9.

If you don't find your problem in this section, consult Solving Common Component Problems (page 735).

 <  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