Programming Examples

Are you a Programmer or Application Developer or a DBA? Take a cup of coffee, sit back and spend few minutes here :)

JToolBar – Floating & Docking

1. Introduction to JToolBar

We know a toolbar houses icon buttons to invoke quick command like File Open, Save. Java swing has a JToolBar class to provide toolbar support for UI based application. In a top-level container like Frame and Dialogs, a toolbar can be housed by making use the BorderLayout manager. We can add JButton, ComboBox, textbox like components to a JToolBar.

2. About the Example

The example is below:

Java Swing JToolbar Example
Java Swing JToolbar Example

Our example has two toolbars in it. One is in the top part of the Frame window and other one is in the bottom part. Each of the toolbar contains three buttons on it and each button is assigned with one image icon. Very first button in the top toolbar shows a message when the user clicks it. When we proceed with the example, we will also learn how to use floating toolbar and study rollover behaviour.

3. Prepare Toolbar Icon Buttons

Preparing tool-bar buttons requires image files of size 24×24 in pixel. One can use paint brush to create these icon sized images. In the below code-snippet 02, we construct ImageIcon instances by loading the images from the G:\Temp folder. Next, in code snippet 3, we create JButton by passing the ImageIcon instances. Now, we have all 6 buttons ready.

4. Create Toolbars

The JToolBar instance of the Java Swing will create the toolbar for us. In the below code snippet, we create two JToolBar objects and add the JButtons created in the previous step to it. For both the toolbar, we disable Rollover and Floating capabilities for now. After we show them in the JFrame, we will enable them. The add method call adds the toolbars to the JFrame. We add the first toolbar to the top of the frame window by specifying the constant BorderLayout.NORTH and the second toolbar to the bottom via the constant Borderlayout.SOUTH.

5. Handle Toolbar Button Event

The toolbar buttons are JButtons. So, it will raise the ActionEvent when the user clicks it. In the below code snippet, we handled this event for the ‘Cut’ Toolbar button. Note, this is the first toolbar button in the tbClip, which stays at the top of the JFrame window. The handler displays a message box stating that the user clicked the toolbar button.

6. JToolBar Rollover

The Rollover property of the toolbar shows button focus animation when the cursor hovers over it. We can turn ON this property via the function call setRollover by passing a boolean true as a param. In our earlier code snippet, we change the first toolbar to enable this rollover mode.

Now, all the toolbar buttons show a highlight animation when the mouse cursor enters it. Below screen explains it:

Swing JToolbar RollOver
Swing JToolbar RollOver

7. Floating JToolBar

JToolBar provides a property Floatable, and this property allows moving the toolbar and docking it to any available edge of the BorderLayout. For example, one can move the toolbar from the top edge and dock it to the left edge. The Floatable property of the toolbar can be enabled via the function call setFloatable. Here in the below code, we enable the floating of the clipboard toolbar:

The below picture shows how the user moves the toolbar, which is on the top side of the JFrame and drags it by its gripper. While the user is dragging it, Java Swing shows the outline of the toolbar and when the mouse cursor is near a valid edge, the orientation of the outline changes, showing that the toolbar can be dropped on this edge. As you see in the picture, the toolbar was dropped on the right edge of the container which is JFrame in our case.

Swing JToolbar - Floating & Docking
Swing JToolbar – Floating & Docking

8. Youtube Demo

9. Code Reference



Categories: Swing

Tags: , , , , ,

Do you like this Example? Please comment about it for others!!

This site uses Akismet to reduce spam. Learn how your comment data is processed.