Java Reference
In-Depth Information
frame.setSize(350, 250);
frame.setVisible(true);
}
};
EventQueue.invokeLater(runner);
}
}
Working with Toolbars: The JToolBar Class
Toolbars are an integral part of the main application windows in a modern user interface.
Toolbars provide users with easy access to the more commonly used commands, which are
usually buried within a hierarchical menuing structure. The Swing component that supports
this capability is the JToolBar .
The JToolBar is a specialized Swing container for holding components. This container can
then be used as a toolbar within your Java applet or application, with the potential for it to
be floating or draggable, outside the main window of the program. JToolBar is a very simple
component to use and understand.
Creating JToolBar Components
There are four constructors for creating JToolBar components:
public JToolBar()
JToolBar jToolBar = new JToolBar();
public JToolBar(int orientation)
JToolBar jToolBar = new JToolBar(JToolBar.VERTICAL);
public JToolBar(String name)
JToolBar jToolBar = new JToolBar("Window Title");
public JToolBar(String name,int orientation)
JToolBar jToolBar = new JToolBar("Window Title", ToolBar.VERTICAL);
By default, a toolbar is created in a horizontal direction. However, you can explicitly set the
orientation by using either of the JToolBar constants of HORIZONTAL and VERTICAL .
Also by default, toolbars are floatable. Therefore, if you create the toolbar with one orien-
tation, the user could change its orientation while dragging the toolbar around outside the
window. When floating, the title will be visible on the toolbar's frame.
Adding Components to a JToolBar
Once you have a JToolBar , you need to add components to it. Any Component can be added to
the toolbar. When dealing with horizontal toolbars, for aesthetic reasons, it's best if the toolbar
components are all roughly the same height. For a vertical toolbar, it's best if they're roughly
the same width. There's only one method defined by the JToolBar class for adding toolbar
Search WWH ::




Custom Search