Programming Examples

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

JSeparator & Control ToolTip

1. About JSeparator & ToolTip

The JSeparator is a general-purpose component in Java Swing. This component will draw splitter line between components. Most people use this to divide the menu items into logical groups. For example, the menu items cut, copy & paste can be grouped between two horizontal splitter lines. The JSeparator can be vertical or horizontal, which we can set at runtime.

Tooltip is a Tile of text which provids quick help to the user. Say, for example, a button displaying a quick text stating what it can do. Most of the UI based programming language supports tooltip. In Java Swing, components display tooltip when the user places the mouse cursor on top of it for a few seconds. To enable this, one should use setToolTipText.

2. About the JSeparator Example

Picture below shows the JSeparator Example with Components tooltip:

JSeparator and Tooltip Example
JSeparator and Tooltip Example

Here, we have sample text boxes in the form. The JSeparator of Java Swing splits each of the text boxes. The splitter is in vertical orientation. Then, we will have tooltips for all the text fields in the window. In the above screen, the Data Of Birth text field is displaying the Tooltip for the user.

3. Add JTextField

Our example requires sample components to add the separator between them. We will also use these components to display the tooltip. Below code uses six text fields. Later we will use these JTextFields and display Tooltip texts.

4. Swing Control Tooltip

All Java Swing Components can display tooltip. The method setToolTipText is exposed via the JComponent to all sub-classes. Recall that components like JTextField, JCheckBox etc are sub-class of the JComponent and one can call setToolTipText method to show the tooltip text. In the below code, we call this method for all our JTextField and pass a text string which will display as tooltip text. The user will hover the mouse cursor over the control and make cursor stand-still for few seconds to see the ToolTip text for that specific component.

5. Create JSeparator

Like check box, radio buttons, text field etc, the JSeparator is also a component in Java Swing. One can add it on the container the way other control can be added. In the below code, we created four JSeperator components. Note, as every separator is a component, if we need ‘N’ number of separators, we need to create ‘N’ number of them. During the construction time, we set the vertical alignment by passing the VERTICAL constant from the JSeparator. Note, for menu items, the horizontal orientation of the JSeparator suits well.

6. JSeparator Size – Width & Height

Java Swing provides Dimension class to denote the 2-dimensional size. This object holds height and width as a positive integer. The swing APIs make use of this object to decide the control’s size. Note, this object defines size, not the control position.

For our example, we need JSeparator between the JTextFields. We fix the width to 2 pixels and height we can calculate from the existing JTextField component. Note the usage of getPreferredSize and setPreferedSize methods. We get the size of the text field as Dimension object and set the matching height for the JSeperator.

7. Add Components with JSeparator

Once JSeparator is ready, we can add it to the container just like how we add other components. In the below code, we add a JSeparator after adding the First Name text box. The flow layout manager knows it should place the separator after displaying the text field. So, the order in which we add the component is also important. For example: we add Sep1 after adding the First Name and before adding the Middle Name. So the separator here splits the First and Middle name TextFields with a vertical line.

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.