Disable Tooltip for a component with an already enabled tooltip in Java

Java 8Object Oriented ProgrammingProgramming

Let’s say we have set the tooltips for all the components using the following method in Java, for example −

setToolTipText("Enter Age");

Here, we have already enabled tooltips for all the components using the setToolTipText(). But, after enabling all of them, we disabled the tooltips using the following −

ToolTipManager.sharedInstance().setEnabled(false);

The following is an example to disable Tooltip for a component with an already enabled 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("Enter Id");
      label2 = new JLabel("Age", SwingConstants.CENTER);
      label2.setToolTipText("Enter Age");
      label3 = new JLabel("Password", SwingConstants.CENTER);
      label3.setToolTipText("Enter Password");
      ToolTipManager.sharedInstance().setEnabled(false);
      JTextField emailId = new JTextField(20);
      JTextField age = new JTextField(20);
      JPasswordField passwd = new JPasswordField();
      passwd.setEchoChar('*');
      frame.add(label1);
      frame.add(label2);
      frame.add(label3);
      frame.add(emailId);
      frame.add(age);
      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);
   }
}

This will produce the following output −

raja
Published on 03-Jun-2019 11:56:29
Advertisements