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:

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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
public class JSeperatorExample extends JFrame { public JSeperatorExample(String title) throws HeadlessException { super(title); //Sample 01: Set Size and Position setBounds(100, 100, 400, 300); Container ControlHost = getContentPane(); ControlHost.setLayout(new FlowLayout()); //Sample 02: Create Five JTextField JTextField FirstName = new JTextField(20); JTextField MiddleName = new JTextField(10); JTextField LastName = new JTextField(20); JTextField DOB = new JTextField(20); JTextField Age = new JTextField(3); |
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.
1 2 3 4 5 6 |
//Sample 03: Set ToolTip FirstName.setToolTipText("First Name"); MiddleName.setToolTipText("Middle Name"); LastName.setToolTipText("Last Name"); DOB.setToolTipText("Date of Birth"); Age.setToolTipText("Age"); |
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.
1 2 3 4 5 |
//Sample 04: Create a Separator JSeparator sep1 = new JSeparator(JSeparator.VERTICAL); JSeparator sep2 = new JSeparator(JSeparator.VERTICAL); JSeparator sep3 = new JSeparator(JSeparator.VERTICAL); JSeparator sep4 = new JSeparator(JSeparator.VERTICAL); |
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.
1 2 3 4 5 |
Dimension d = FirstName.getPreferredSize(); sep1.setPreferredSize(new Dimension(2, d.height)); sep2.setPreferredSize(new Dimension(2, d.height)); sep3.setPreferredSize(new Dimension(2, d.height)); sep4.setPreferredSize(new Dimension(2, d.height)); |
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.
1 2 3 4 5 6 7 8 9 10 |
//Sample 05: Add Text Field to Frame ControlHost.add(FirstName); ControlHost.add(sep1); ControlHost.add(MiddleName); ControlHost.add(sep2); ControlHost.add(LastName); ControlHost.add(sep3); ControlHost.add(DOB); ControlHost.add(sep4); ControlHost.add(Age); |
8.Youtube Demo
9. Code Reference
9.1 MainEntry.java
1 2 3 4 5 6 7 8 9 |
package tube.coding.examples; public class MainEntry { public static void main(String[] args) { //Sample 07: Create Instance of JFrameDemo JSeperatorExample frame = new JSeperatorExample("ToolTip & JSeparator Example"); frame.setVisible(true); } } |
9.2 JSeperatorExample.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 |
package tube.coding.examples; import java.awt.Container; import java.awt.Dimension; import java.awt.FlowLayout; import java.awt.HeadlessException; import javax.swing.JFrame; import javax.swing.JSeparator; import javax.swing.JTextField; public class JSeperatorExample extends JFrame { public JSeperatorExample(String title) throws HeadlessException { super(title); //Sample 01: Set Size and Position setBounds(100, 100, 400, 300); Container ControlHost = getContentPane(); ControlHost.setLayout(new FlowLayout()); //Sample 02: Create Five JTextField JTextField FirstName = new JTextField(20); JTextField MiddleName = new JTextField(10); JTextField LastName = new JTextField(20); JTextField DOB = new JTextField(20); JTextField Age = new JTextField(3); //Sample 03: Set ToolTip FirstName.setToolTipText("First Name"); MiddleName.setToolTipText("Middle Name"); LastName.setToolTipText("Last Name"); DOB.setToolTipText("Date of Birth"); Age.setToolTipText("Age"); //Sample 04: Create a Separator JSeparator sep1 = new JSeparator(JSeparator.VERTICAL); JSeparator sep2 = new JSeparator(JSeparator.VERTICAL); JSeparator sep3 = new JSeparator(JSeparator.VERTICAL); JSeparator sep4 = new JSeparator(JSeparator.VERTICAL); Dimension d = FirstName.getPreferredSize(); sep1.setPreferredSize(new Dimension(2, d.height)); sep2.setPreferredSize(new Dimension(2, d.height)); sep3.setPreferredSize(new Dimension(2, d.height)); sep4.setPreferredSize(new Dimension(2, d.height)); //Sample 05: Add Text Field to Frame ControlHost.add(FirstName); ControlHost.add(sep1); ControlHost.add(MiddleName); ControlHost.add(sep2); ControlHost.add(LastName); ControlHost.add(sep3); ControlHost.add(DOB); ControlHost.add(sep4); ControlHost.add(Age); } } |
Categories: Swing
Tags: Dimension, getPreferredSize, setPreferedSize, setTooltipText, VERTICAL