Using ToolTip

Education is not limited to just classrooms. It can be gained anytime, anywhere... - Ravi Ranjan (M.Tech-NIT)

How to use ToolTip in Java Swing

You can create a tool tip for any JComponent with setToolTipText() method. This method is used to set up a tool tip for the component.

For example, to add tool tip to PasswordField, you need to add only one line of code:

  1. field.setToolTipText("Enter your Password");  

Simple ToolTip Example

  1. import javax.swing.*;    
  2. public class ToolTipExample {  
  3.     public static void main(String[] args) {    
  4.      JFrame f=new JFrame("Password Field Example");    
  5.      //Creating PasswordField and label  
  6.      JPasswordField value = new JPasswordField();   
  7.      value.setBounds(100,100,100,30);    
  8.      value.setToolTipText("Enter your Password");  
  9.      JLabel l1=new JLabel("Password:");    
  10.      l1.setBounds(20,10080,30);    
  11.      //Adding components to frame  
  12.      f.add(value);  f.add(l1);  
  13.      f.setSize(300,300);    
  14.      f.setLayout(null);    
  15.      f.setVisible(true);      
  16. }  
  17. }  


Java How to use tool tip 1