8.5 The JWindow Class


JWindow is an extension of java.awt.Window that uses a JRootPane as its single component. Other than this core distinction, JWindow does not change anything defined by the Window class.

In AWT, one common reason for using the Window class was to create a pop-up menu. Since Swing explicitly provides a JPopupMenu class (see Chapter 14), there is no need to extend JWindow for this purpose. The only time you'll use JWindow is if you have something that needs to be displayed in its own window without the adornments added by JFrame. Remember, this means that the window can only be moved or closed programmatically (or via the user's platform-specific window manager controls, if available).

One possible use for JWindow would be to display a splash screen when an application is starting up. Many programs display screens like this, containing copyright information, resource loading status, etc. Here's such a program:

// SplashScreen.java // import java.awt.*; import javax.swing.*; public class SplashScreen extends JWindow {   private int duration;   public SplashScreen(int d) {     duration = d;   }   // A simple little method to show a title screen in the center of the screen for   // the amount of time given in the constructor   public void showSplash( ) {     JPanel content = (JPanel)getContentPane( );     content.setBackground(Color.white);     // Set the window's bounds, centering the window.     int width = 450;     int height =115;     Dimension screen = Toolkit.getDefaultToolkit( ).getScreenSize( );     int x = (screen.width-width)/2;     int y = (screen.height-height)/2;     setBounds(x,y,width,height);     // Build the splash screen.     JLabel label = new JLabel(new ImageIcon("oreilly.gif"));     JLabel copyrt = new JLabel       ("Copyright 2002, O'Reilly & Associates", JLabel.CENTER);     copyrt.setFont(new Font("Sans-Serif", Font.BOLD, 12));     content.add(label, BorderLayout.CENTER);     content.add(copyrt, BorderLayout.SOUTH);     Color oraRed = new Color(156, 20, 20,  255);     content.setBorder(BorderFactory.createLineBorder(oraRed, 10));     // Display it.     setVisible(true);     // Wait a little while, maybe while loading resources.     try { Thread.sleep(duration); } catch (Exception e) {}     setVisible(false);   }   public void showSplashAndExit( ) {     showSplash( );     System.exit(0);   }   public static void main(String[] args) {     // Throw a nice little title page up on the screen first.     SplashScreen splash = new SplashScreen(10000);     // Normally, we'd call splash.showSplash( ) and get on with the program.     // But, since this is only a test...     splash.showSplashAndExit( );   } }

All this program does is create a JWindow containing a pair of labels and display it in the center of the screen. In a real application, the title screen might be displayed while various system resources are being loaded (consider using a ProgressMonitor in this case). When run, this example displays a simple window in the center of the screen, as shown in Figure 8-10.

Figure 8-10. JWindow used as a splash screen
figs/swng2.0810.gif

8.5.1 Properties

JWindow defines the properties shown in Table 8-9. The contentPane , glassPane, and layeredPane are really properties of JRootPane (described earlier in the chapter). Direct access is provided for convenience. Unlike JFrame (and JApplet, described below), JWindow does not provide direct access to the root pane's menu bar. This is just an indication of JWindow's intended usage. If you have some compelling reason to display a menu bar on a JWindow, you can always access it through the root pane or just add it as a component.

Table 8-9. JWindow properties

Property

Data type

get

is

set

Default value

accessibleContexto

AccessibleContext

·

   

JWindow.AccessibleJWindow( )

contentPaneo

Container

·

 

·

From rootPane

glassPaneo

Component

·

 

·

From rootPane

layeredPaneo

JLayeredPane

·

 

·

From rootPane

layouto

LayoutManager

·

 

·

BorderLayout( )

rootPaneo, *

JRootPane

·

 

·

JRootPane( )

rootPaneCheckingEnabledp

boolean

 

·

·

true

ooverridden, pprotected

*The setRootPane() method is protected.

See also the java.awt.Window class.

The layout property is listed here because JWindow overrides setLayout( ) to throw an Error if an attempt is made to change the layout manager, rather than set the layout manager of the window's content pane.

The rootPane property is set to a new instance of JRootPane when the frame is created and cannot be changed using public methods.

8.5.2 Constructors

public JWindow( )

Create a new, invisible window associated with no particular owner. This uses a package-private method in SwingUtilities to make a "fake" frame that serves as the owner. This makes this window a top-level window with no focus dependencies.

public JWindow(JFrame frame)
public JWindow(Window window)

Create a new, invisible window associated with the given frame or window. A window created with a valid (i.e., non-null) association is focusable only when the associated frame or window is visible on the screen.

public JWindow(GraphicsConfiguration gc)
public JWindow(Window window, GraphicsConfiguration gc)

Create a new, invisible window (possibly associated with the given window) using the given graphics configuration. The GraphicsConfiguration object lets you create windows on things such as virtual screens.



Java Swing
Graphic Java 2: Mastering the Jfc, By Geary, 3Rd Edition, Volume 2: Swing
ISBN: 0130796670
EAN: 2147483647
Year: 2001
Pages: 289
Authors: David Geary

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