31.5. Table Renderers and Editors

 
[Page 918 ( continued )]

28.2. Swing Container Structures

User interface components like JButton cannot be displayed without being placed in a container. A container is a component that is capable of containing other components. You do not display a user interface component; you place it in a container, and the container displays the components it contains.

The base class for all containers is java.awt.Container , which is a subclass of java.awt.Component . The Container class has the following essential functions:

  • It adds and removes components using various add and remove methods .

  • It maintains a layout property for specifying a layout manager that is used to lay out components in the container. Every container has a default layout manager.

  • It provides registration methods for the java.awt.event.ContainerEvent .

In AWT programming, the java.awt.Frame class is used as a top-level container for Java applications, the java.awt.Applet class is used for all Java applets, and java.awt.Dialog is used for dialog windows . These classes do not work properly with Swing lightweight components. Special versions of Frame , Applet , and Dialog named JFrame , JApplet , and JDialog have been developed to accommodate Swing components. JFrame is a subclass of Frame , JApplet is a subclass of Applet , and JDialog is a subclass of Dialog . JFrame and JApplet inherit all the functions of their heavyweight counterparts, but they have a more complex internal structure with several layered panes, as shown in Figure 28.1.

Figure 28.1. Swing top-level containers use layers of panes to group lightweight components and make them work properly.



[Page 919]

javax.swing.JRootPane is a lightweight container used behind the scenes by Swing's top-level containers, such as JFrame , JApplet , and JDialog . javax.swing.JLayeredPane is a container that manages the optional menu bar and the content pane. The content pane is an instance of Container . By default, it is a JPanel with BorderLayout . This is the container where the user interface components are added. To obtain the content pane in a JFrame or in a JApplet , use the getContentPane() method. If you wish to set an instance of Container to be a new content pane, use the setContentPane method. The glass pane floats on top of everything. javax.swing.JGlassPane is a hidden pane by default. If you make the glass pane visible, then it's like a sheet of glass over all the other parts of the root pane. It's completely transparent unless you implement the glass pane's paint method so that it does something, and it intercepts input events for the root pane. In general, JRootPane , JLayeredPane , and JGlassPane are not used directly.

Now let us review the three most frequently used Swing containers: JFrame , JApplet , and JPanel .

28.2.1. JFrame

JFrame , a Swing version of Frame , is a top-level container for Java graphics applications. Like Frame , JFrame is displayed as a standalone window with a title bar and a border. The following properties are often useful in JFrame :

  • contentPane is the content pane of the frame.

  • iconImage is the image that represents the frame. This image replaces the default Java image on the frame's title bar and is also displayed when the frame is minimized. This property type is Image . You can get an image using the ImageIcon class, as follows :

     Image image = (   new   ImageIcon(filename)).getImage(); 
  • jMenuBar is the optional menu bar for the frame.

  • resizable is a boolean value indicating whether the frame is resizable. The default value is true .

  • title specifies the title of the frame.

28.2.2. JApplet

JApplet is a Swing version of Applet . Since it is a subclass of Applet , it has all the functions required by the Web browser. Here are the four essential methods defined in Applet :

  // Called by the browser when the Web page containing   // this applet is initially loaded    public void   init()  // Called by the browser after the init() method and   // every time the Web page is visited.    public void   start()  // Called by the browser when the page containing this   // applet becomes inactive.    public void   stop()  // Called by the browser when the Web browser exits.    public void   destroy() 

Additionally, JApplet has the contentPane and jMenuBar properties, among others. As with JFrame , you do not place components directly into JApplet ; instead you place them into the content pane of the applet. The Applet class cannot have a menu bar, but the JApplet class allows you to set a menu bar using the setJMenuBar method.


[Page 920]

Note

When an applet is loaded, the Web browser creates an instance of the applet by invoking the applet's no-arg constructor. So the constructor is invoked before the init method.


28.2.3. JPanel

Panels act as subcontainers for grouping user interface components. javax.swing.JPanel is different from JFrame and JApplet . First, JPanel is not a top-level container; it must be placed inside another container, and it can be placed inside another JPanel . Second, since JPanel is a subclass of JComponent , it is a lightweight component, but JFrame and JApplet are heavyweight components.

JPanel is a Swing version of Panel , but it is not a subclass of Panel . Nevertheless, you can use JPanel the same way you use Panel . As a subclass of JComponent , JPanel can take advantage of JComponent , such as double buffering and borders. You should draw figures on JPanel rather than JFrame or JApplet , because JPanel supports double buffering, which is the technique for eliminating flickers.

 


Introduction to Java Programming-Comprehensive Version
Introduction to Java Programming-Comprehensive Version (6th Edition)
ISBN: B000ONFLUM
EAN: N/A
Year: 2004
Pages: 503

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