
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Display Multiple Lines of Text in a Component's Tooltip with Java
Let’s first see how we set text in a components tooltip −
JLabel label3 = new JLabel("Password", SwingConstants.CENTER); label3.setToolTipText("Enter Password");
To display multiple lines of text in a tooltip, use <html>. Here, we have used the HTML <br> tag for new line and that would create multiple lines of text in the tooltip −
label3.setToolTipText("<html>" + "This will create multiple lines for the" + "<br>" + "component! Yay!" + "</html>");
The following is an example to display multiple lines of text in a component’s tooltip −
Example
package my; import java.awt.GraphicsEnvironment; import java.awt.GridLayout; import java.awt.Point; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JPasswordField; import javax.swing.JTextField; import javax.swing.SwingConstants; import javax.swing.ToolTipManager; public class SwingDemo { public static void main(String[] args) throws Exception { JFrame.setDefaultLookAndFeelDecorated(true); JFrame frame = new JFrame("Register!"); JLabel label1, label2, label3; frame.setLayout(new GridLayout(2, 2)); label1 = new JLabel("Id", SwingConstants.CENTER); label1.setToolTipText("<html>" + "Add the id given to you" + "<br>" + "in the beginning!" + "</html>"); label2 = new JLabel("Rank", SwingConstants.CENTER); label2.setToolTipText("Enter rank"); label3 = new JLabel("Password", SwingConstants.CENTER); label3.setToolTipText("Enter Password"); ToolTipManager.sharedInstance().setEnabled(false); JTextField emailId = new JTextField(20); JTextField rank = new JTextField(20); JPasswordField passwd = new JPasswordField(); passwd.setEchoChar('*'); frame.add(label1); frame.add(label2); frame.add(label3); frame.add(emailId); frame.add(rank); frame.add(passwd); Point center = GraphicsEnvironment.getLocalGraphicsEnvironment().getCenterPoint(); int width = 500; int height = 200; frame.setBounds(center.x - width / 2, center.y - height / 2, width, height); frame.setVisible(true); } }
The output is as follows. Here, we are displaying the multi-line tooltip text visible on mouse hover −
Advertisements