32.2. Relational Database Systems

 
[Page 976 ( continued )]

29.4. JToolBar

In user interfaces, a toolbar is often used to hold commands that also appear in the menus . Frequently used commands are placed in a toolbar for quick access. Clicking a command in the toolbar is faster than choosing it from the menu.

Swing provides the JToolBar class as the container to hold tool bar components . JToolBar uses BoxLayout to manage components by default. You can set a different layout manager if desired. The components usually appear as icons. Since icons are not components, they cannot be placed into a tool bar directly. Instead you place buttons into the tool bar and set the icons on the buttons . An instance of JToolBar is like a regular container. Often it is placed in the north, west, or east of a container of BorderLayout .


[Page 977]

The following properties in the JToolBar class are often useful:

  • orientation specifies whether the items in the tool bar appear horizontally or vertically. The possible values are JToolBar.HORIZONTAL and JToolBar.VERTICAL . The default value is JToolBar.HORIZONTAL .

  • floatable is a boolean value that specifies whether the tool bar can be floated. By default, a tool bar is floatable.

Listing 29.3 gives an example that creates a JToolBar to hold three buttons with the icons representing the commands New, Open , and Print, as shown in Figure 29.5.

Figure 29.5. The tool bar contains the icons representing the commands New, Open, and Print.


Listing 29.3 shows the program.

Listing 29.3. ToolBarDemo.java
 1   import   javax.swing.*;  2   import   java.awt.*;  3  4   public class   ToolBarDemo   extends   JApplet {  5   private   JButton jbtNew =   new   JButton(  6   new   ImageIcon(getClass().getResource(   "image/new.gif"   )));  7   private   JButton jbtOpen =   new   JButton(  8   new   ImageIcon(getClass().getResource(   "image/open.gif"   )));  9   private   JButton jbPrint =   new   JButton( 10   new   ImageIcon(getClass().getResource(   "image/print.gif"   ))); 11 12   public   ToolBarDemo() { 13  JToolBar jToolBar1 =   new   JToolBar(   "My Tool Bar"   );  14  jToolBar1.setFloatable(   true   );  15  jToolBar1.add(jbtNew);  16  jToolBar1.add(jbtOpen);  17  jToolBar1.add(jbPrint);  18 19      jbtNew.setToolTipText(   "New"   ); 20      jbtOpen.setToolTipText(   "Open"   ); 21      jbPrint.setToolTipText(   "Print"   ); 22 23      jbtNew.setBorderPainted(   false   ); 24      jbtOpen.setBorderPainted(   false   ); 25      jbPrint.setBorderPainted(   false   ); 26 27  add(jToolBar1, BorderLayout.NORTH);  28    } 29  } 

A JToolBar is created in line 13. The tool bar is a container with BoxLayout by default. Using the orientation property, you can specify whether components in the tool bar are organized horizontally or vertically. By default, it is horizontal.


[Page 978]

By default, the tool bar is floatable, and a floatable controller is displayed in front of its components. You can drag the floatable controller to move the tool bar to different locations of the window or can show the tool bar in a separate window, as shown in Figure 29.6.

Figure 29.6. The toolbar buttons are floatable.

You can also set a title for the floatable tool bar, as shown in Figure 29.7(a). To do so, create a tool bar using the JToolBar(String title) constructor. If you set floatable false, the floatable controller is not displayed, as shown in Figure 29.7(b). If you set a border (e.g., a line border), as shown in Figure 29.7(c), the line border is displayed and the floatable controller is not displayed.

Figure 29.7. The toolbar buttons can be customized in many forms.

Tip

For the floatable feature to work properly, do the following: (1) place a tool bar to one side of the container of BorderLayout and add no components to the other sides; (2) don't set border on a tool bar. Setting a border would make it non-floatable.


 


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