28.12.2012 Views

Core JFC Java Foundation Classes 2nd edition - Read

Core JFC Java Foundation Classes 2nd edition - Read

Core JFC Java Foundation Classes 2nd edition - Read

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

The Menu Bar and the Menu System 713<br />

bar may start life attached to the application frame and then be detached, or<br />

it may be created in a floating state.<br />

Creating a Floatable Toolbar<br />

There are several methods that you need to use if you want to create a floatable<br />

toolbar and make it float under program control:<br />

public void setFloatable(boolean b);<br />

public void setFloating(boolean b, Point p);<br />

public void setFloatingLocation(int x, int y);<br />

A toolbar must be floatable in order for it to become detached from the<br />

application frame. You can use the setFloatable method to make the toolbar<br />

floatable but, in fact, all toolbars are floatable by default so, if you don't<br />

want your applications toolbar to float, you need to use setFloatable<br />

(false). Assuming that a toolbar is floatable, it won't actually detach<br />

itself until setFloating is called. This method serves the dual purpose of<br />

detaching the toolbar from the frame when its first argument is true and reattaching<br />

or docking it later when it is called with its first argument set to<br />

false. The setFloatingLocation method is used to control where the<br />

toolbar is placed when it is detached. You'll see how this method is used at<br />

the end of this chapter.<br />

Floating and docking toolbars are actually supported not by JToolBar<br />

itself, but by the basic toolbar UI class. The setFloatable method belongs<br />

to JToolBar, but the other two, along with some others that you'll see<br />

shortly, are provided by the BasicToolBarUI class, not by JToolBar. Fortunately,<br />

however, the various look-and-feel implementations either use the<br />

basic toolbar UI class directly or extend it, so their toolbars are all built<br />

around BasicToolBarUI. Therefore, all three standard look-and-feels support<br />

floating and docking toolbars. You'll notice that all the examples in this<br />

section check that the UI is an instance of BasicToolBarUI before<br />

attempting to use either of the last two methods, to avoid an exception should<br />

a new look-and-feel with a separate UI be introduced.<br />

Although you can detach and reattach floatable toolbars under program<br />

control, it is usually the user that controls whether the toolbar is floating or<br />

docked. When it is docked, as you have seen, the toolbar normally resides<br />

near the top of the application frame. When it is floating, however, it is

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!