- Colored ToolTips
- Detachable/Non-Detachable Toolbars
- Individualized List ToolTips
- Non-Auto-Resize Table Columns
- Table Header Icons
Detachable/Non-Detachable Toolbars
A GUI can be enhanced by providing a toolbar (with various components). For example, Figure 2 illustrates a simple GUI that consists of a menu and a toolbar.
A toolbar can be used to enhance a GUI.
Toolbars are normally docked (attached) to the top, bottom, left, or right of a GUI's frame window. By default, a toolbar can be detached from its docked position. To detach the toolbar, position the mouse cursor over handle (the dotted rectangle displayed using the Swing look and feel) on the left side of the toolbar and perform a drag operation (by holding down the left mouse button while moving the mouse cursor). When the toolbar is detached, it turns into a floating window, as illustrated in Figure 3.
When a toolbar is detached, it turns into a floating window. To reattach the toolbar, double-click the System menu in the floating window's upper-left corner.
A toolbar can be "forced" to remain docked—that is, preventing it from being detached—by calling JToolBar's setFloatable method with a false argument. This is demonstrated by the following code fragment (which assumes that tb is referencing a previously created JToolBar object):
tb.setFloatable (false);After calling tb.setFloatable (false);, the handle is gone. The absence of the handle means that the toolbar can no longer be detached. Figure 4 illustrates the resulting toolbar.
A non-detachable toolbar doesn't display a handle.
Listing 2 presents source code to a DNDToolbars application that demonstrates detachable/non-detachable toolbars. (The GUIs in Figures 2 through 4 were generated by this application.)
Listing 2 The DNDToolbars application source code
// DNDToolbars.java import javax.swing.*; import java.awt.*; import java.awt.event.*; class DNDToolbars extends JFrame { JToolBar tb; DNDToolbars (String title) { // Pass the title argument to the JFrame superclass so that the // contents of title appear in the JFrame's title bar. super (title); // Exit the application by calling System.exit when the user // either selects Close from the System menu or clicks the X // button. setDefaultCloseOperation (EXIT_ON_CLOSE); // Create an object from a subclass of AbstractAction and // assign its reference to a1. This is legal because // AbstractAction implements Action. As a result, the object // is also an Action. This action is executed if the user // either selects Detachable from the menu or clicks the // toolbar's Detachable button. Action a1 = new AbstractAction () { // This instance initializer assigns the // action's name as soon as the object is // created. { putValue (Action.NAME, "Detachable"); } public void actionPerformed (ActionEvent e) { // Make the JToolBar detachable. tb.setFloatable (true); } }; // Create an object from a subclass of AbstractAction and assign // its reference to a1. This is legal because AbstractAction // implements Action. As a result, the object is also an // Action. This action is executed if the user either selects // Non-Detachable from the menu or clicks the toolbar's // Non-Detachable button. Action a2 = new AbstractAction () { // This instance initializer assigns the // action's name as soon as the object is // created. { putValue (Action.NAME, "Non-Detachable"); } public void actionPerformed (ActionEvent e) { // Make the JToolBar non-detachable. tb.setFloatable (false); } }; // Create a file menu JMenu fileMenu = new JMenu ("File"); // Create a "Detachable" menu item, assign a as this menu item's // action, and add the item to the File menu. JMenuItem mi = new JMenuItem (); mi.setAction (a1); fileMenu.add (mi); // Create a "Non-Detachable" menu item, assign a as this menu // item's action, and add the item to the File menu. mi = new JMenuItem ("Non-Detachable"); mi.setAction (a2); fileMenu.add (mi); // Create a menu bar and add the File menu to this menu bar. JMenuBar mb = new JMenuBar (); mb.add (fileMenu); // Establish the menu bar as this program's menu bar. setJMenuBar (mb); // Create the JToolBar. tb = new JToolBar (); // Create and add "Detachable" JButton to the JToolBar. JButton b = new JButton (); b.setAction (a1); tb.add (b); // Create and add "Non-Detachable" JButton to the JToolBar. b = new JButton (); b.setAction (a2); tb.add (b); // Place the JToolBar in a JPanel to give it a more natural // size. JPanel p = new JPanel (); p.setLayout (new BorderLayout ()); p.add (tb, BorderLayout.WEST); // Add the JPanel to the North region of the JFrame's content // pane. getContentPane ().add (p, BorderLayout.NORTH); // Set the size of the JFrame window. setSize (350, 150); // Show the JFrame window. setVisible (true); } public static void main (String [] args) { new DNDToolbars ("Detachable/Non-Detachable Toolbars"); } }