Swing menu and toolbar (6)

Source: Internet
Author: User

6.3 use the toolbar: JToolBar class
The toolbar is the main part of the main program window in the Modern user interface. The toolbar provides users with simple access to common commands, which is usually built into a hierarchical menu structure. The Swing component that supports this function is JToolBar.

JToolBar is a special Swing container that stores components. This container can be used as a toolbar in our Java Applet or program, and can be floating or dragged out of the main window of the program. JToolBar is a simple component that is easy to use and understand.

6.3.1 create a JToolBar component
There are four constructors that can be used to create the JToolBar component:

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, the ToolBar is created horizontally. However, we can use the constant HORIZONTAL and vertical of the JToolBar to display the specified direction.

By default, the toolbar can be floating. Therefore, if we create a toolbar in the horizontal direction, you can drag the toolbar around the window to change the toolbar direction.

6.3.2 add components to JToolBar
Once we have a JToolBar, we need to add components to it. Any Component can be added to the toolbar. When processing the horizontal toolbar, it is best if the toolbar components are roughly the same height for aesthetic reasons. For vertical toolbar, it is best if toolbar components have roughly the same width. The JToolBar class defines only one method to add a toolbar project. Other methods, such as add (Component), are inherited by Container. In addition, you can add separators to the toolbar.

Public JButton add (Action action );
Public void addSeparator ();
Public void addSeparator (Dimension size); when the add (Action) method of JToolBar is used, the added Action is closed in a JButton object. This is different from adding an Action to the JMenu or JPopupMenu component. In the latter case, the JMenuItem object is added. For JMenu and JPopupMenu, adding Action in this way is not recommended in Javadoc of the class. For separators, if we do not specify the size, the installed view will force the default size settings.

You can use the following method to remove a component from the toolbar:

Public void remove (Component component) 6.3.3 JToolBar attribute
Table 6-15 lists the nine attributes defined by JToolBar.

JToolBar attributes

Attribute name
Data Type
Accessibility
 
AccessibleContext
AccessibleContext
Read-Only
 
BorderPainted
Boolean
Read/write binding
 
Floatable
Boolean
Read/write binding
 
Layout
LayoutManager
Write only
 
Margin
Insets
Read/write binding
 
Orientation
Int
Read/write binding
 
Rolover
Boolean
Read/write binding
 
UI
ToolBarUI
Read/write
 
UIClassID
String
Read-Only
 

By default, the border of the JToolBar is drawn. If we do not want to draw a border, we can set the borderPainted attribute to false. If the borderPainted attribute is not used, modify the border attribute (the attribute inherited by the super class JComponent ).

The orientation attribute can only be set to the HORIZONTAL or VERTICAL constant of the JToolBar. If other values are used, IllegalArgumentException is thrown. Changing the direction will change the layout manager of the toolbar. If we use setLayout () to directly modify the layout manager, changing the direction will undo our layout manager.

As mentioned above, the toolbar is floating by default. This means that you can drag the toolbar and place it elsewhere. To drag the toolbar, select the blank area of the toolbar. The toolbar can then stay in the original program window, float within the main window, or drag to other parts of the original program window. If the layout manager of the original window is BorderLayout, the side that can be dragged is the side of the layout manager without components. (We cannot place the toolbar in the center of the window .) Otherwise, the toolbar is dragged to the last point of the container. Figure 6-10 shows the different phases of the drag and parking process.

The rolover attribute defines the behavior that is specific to the perception when a user moves on different components in the toolbar. This behavior involves different colors and borders.

6.3.4 process JToolBar events
There are no JToolBar-specific events. We need to associate the listener to each item on the JToolBar that responds to user interaction. Of course, JToolBar is a iner, so we can also listen to its events.

6.3.5 custom JToolBar View
Each installable Swing view provides its own JToolBar appearance and default set of UIResource values. Most of the appearances are controlled by the actual components in the toolbar. Figure 6-11 shows the appearance of the pre-installed Motif, Windows, and Ocean JToolBar components. Each toolbar has five JButton components, with a separator between the fourth component and the fifth component.

 

The UIResource attributes of JToolBar are listed in Table 6-16. The JToolBar component has 22 different attributes.

JToolBar UIResource Element


Attribute string
Object Type
 
ToolBar. actionMap
ActionMap
 
ToolBar. ancestorInputMap
InputMap
 
ToolBar. background
Color
 
ToolBar. border
Border
 
ToolBar. borderColor
Color
 
ToolBar. darkShadow
Color
 
ToolBar.doc kingBackground
Color
 
ToolBar.doc ingForeground
Color
 
ToolBar. floatingBackground
Color
 
ToolBar. floatingForeground
Color
 
ToolBar. font
Font
 
ToolBar. foreground
Color
 
ToolBar. handleIcon
Icon
 
ToolBar. highlight
Color
 
ToolBar. isrolover
Boolean
 
ToolBar. light
Color
 
ToolBar. nonroloverborder
Border
 
ToolBar. roloverborder
Border
 
ToolBar. separatorSize
Dimension
 
ToolBar. shadow
Color
 
ToolBarSeparatorUI
String
 
ToolBarUI
String
 

6.3.6 complete JToolBar use example
LIST 6-8 demonstrates a complete JToolBar example, which generates a toolbar with multiple diamond buttons. This program also reused the ShowAction defined in the menu example in the previous section 6-2.

In this example, the rolover attribute is allowed to demonstrate the differences in the current view. Figure 6-12 shows the output when we move the mouse over different buttons.

Package net. ariel. ch06; import java. awt. BorderLayout;
Import java. awt. Color;
Import java. awt. Container;
Import java. awt. EventQueue;
Import java. awt. event. ActionEvent;
Import java. awt. event. ActionListener; import javax. swing. Action;
Import javax. swing. Icon;
Import javax. swing. JButton;
Import javax. swing. JFrame;
Import javax. swing. JScrollPane;
Import javax. swing. JTextArea;
Import javax. swing. JToolBar; import net. ariel. ch04.DiamondIcon; public class ToolBarSample {private static final int COLOR_POSITION = 0;
Private static final int STRING_POSITION = 1;
Static Object buttonColors [] [] = {
{Color. RED, "RED "},
{Color. BLUE, "BLUE "},
{Color. GREEN, "GREEN "},
{Color. BLACK, "BLACK "},
Null, // separator
{Color. CYAN, "CYAN "}
}; Public static class TheActionListener implements ActionListener {
Public void actionreceivmed (ActionEvent event ){
System. out. println (event. getActionCommand ());
}
}
/**
* @ Param args
*/
Public static void main (String [] args ){
// TODO Auto-generated method stub Runnable runner = new Runnable (){
Public void run (){
JFrame frame = new JFrame ("JToolBar Example ");
Frame. setdefaclocloseoperation (JFrame. EXIT_ON_CLOSE );
ActionListener actionListener = new TheActionListener (); JToolBar toolbar = new JToolBar ();
Toolbar. setrolover (true); for (Object [] color: buttonColors ){
If (color = null ){
Toolbar. addSeparator ();
}
& Nbs

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.